Files
Davide Silvestri fc6bfb299b feat(database): format AI field text output as rich text (#6221)
* feat(database): format AI field text output as rich text

AI fields with text output can store rich text, reusing the rich long
text editor without mentions or images. New AI fields default to it in
the create form; existing fields, API clients and old imports stay
plain. Rich fields add a one-line formatting instruction to the prompt.

Also:
- forward row-modal edits of AI fields, which were never saved
- keep image markdown as literal text when images are off, instead of
  reducing it to its alt text and losing the URL on save
- escape user file image references as `![alt]\[name]` so a reference
  nested in another's alt text can't stay live
- return 400 instead of 500 when a PATCH would make a primary field
  rich text

Closes #6220

* fix: preserve reference image URLs in rich text

Defined Markdown image tokens carry their destination and title separately
from the raw reference marker. Keep that data as self-contained literal
inline text when image nodes are disabled, and align the preview behavior.

Add parser, AI row persistence, and browser regressions while preserving
internal file references and the existing inline-image path.

* fix: handle AI field changes during generation
2026-10-05 09:17:09 +02:00

87 lines
2.7 KiB
JavaScript

import { TestApp } from '@baserow/test/helpers/testApp'
import RowCardFieldRichText from '@baserow/modules/database/components/card/RowCardFieldRichText'
describe('RowCardFieldRichText component', () => {
let testApp = null
let store = null
beforeEach(() => {
testApp = new TestApp()
store = testApp.store
})
afterEach(async () => {
await testApp.afterEach()
})
const mountComponent = (value, props = {}) =>
testApp.mount(RowCardFieldRichText, {
props: { value, workspaceId: 10, ...props },
})
test('renders the Markdown preview', async () => {
const wrapper = await mountComponent('## Card title\n\n- item')
expect(wrapper.find('h2').text()).toBe('Card title')
expect(wrapper.find('ul li').text()).toBe('item')
})
test('renders clickable links opening a new tab', async () => {
const wrapper = await mountComponent('[Baserow](https://baserow.io)')
const link = wrapper.find('a')
expect(link.attributes('href')).toBe('https://baserow.io')
expect(link.attributes('target')).toBe('_blank')
expect(link.attributes('rel')).toBe('noopener noreferrer nofollow')
})
test('resolves mentions against the workspace users', async () => {
await store.dispatch('workspace/forceCreate', {
id: 10,
name: 'Workspace',
users: [{ user_id: 5, name: 'Jane Doe' }],
})
const wrapper = await mountComponent('ping @5 and @99')
const mentions = wrapper.findAll('.rich-text-editor__mention')
expect(mentions).toHaveLength(1)
expect(mentions[0].text()).toBe('@Jane Doe')
expect(mentions[0].attributes('data-id')).toBe('5')
expect(wrapper.text()).toContain('and @99')
})
test('keeps mentions as text when mentions are disabled', async () => {
await store.dispatch('workspace/forceCreate', {
id: 10,
name: 'Workspace',
users: [{ user_id: 5, name: 'Jane Doe' }],
})
const wrapper = await mountComponent('ping @5', { enableMentions: false })
expect(wrapper.find('.rich-text-editor__mention').exists()).toBe(false)
expect(wrapper.text()).toBe('ping @5')
})
test.each([true, false])(
'renders an image only when enableImages is %s',
async (enableImages) => {
const wrapper = await mountComponent(
'![a][abc123_def456.png](https://example.com/a.png)',
{ enableImages }
)
expect(wrapper.find('img').exists()).toBe(enableImages)
}
)
test('keeps raw HTML in cell values inert', async () => {
const wrapper = await mountComponent(
'<script>window.hacked = true</script><img src="x" onerror="window.hacked = true">'
)
expect(wrapper.find('script').exists()).toBe(false)
expect(wrapper.find('img').exists()).toBe(false)
expect(window.hacked).toBeUndefined()
})
})