fix(editor): markdown code preprocessor should handle link correctly (#11671)
Close [BS-3117](https://linear.app/affine-design/issue/BS-3117/代码粘贴后出现多余的-和-符号)
This commit is contained in:
@@ -4091,4 +4091,55 @@ hhh
|
|||||||
});
|
});
|
||||||
expect(nanoidReplacement(rawBlockSnapshot)).toEqual(blockSnapshot);
|
expect(nanoidReplacement(rawBlockSnapshot)).toEqual(blockSnapshot);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should not wrap url with angle brackets if it is not a url', async () => {
|
||||||
|
const markdown = 'prompt: How many people will live in the world in 2040?';
|
||||||
|
const sliceSnapshot: SliceSnapshot = {
|
||||||
|
type: 'slice',
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: 'block',
|
||||||
|
id: 'matchesReplaceMap[0]',
|
||||||
|
flavour: 'affine:note',
|
||||||
|
props: {
|
||||||
|
xywh: '[0,0,800,95]',
|
||||||
|
background: DefaultTheme.noteBackgrounColor,
|
||||||
|
index: 'a0',
|
||||||
|
hidden: false,
|
||||||
|
displayMode: NoteDisplayMode.DocAndEdgeless,
|
||||||
|
},
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
type: 'block',
|
||||||
|
id: 'matchesReplaceMap[1]',
|
||||||
|
flavour: 'affine:paragraph',
|
||||||
|
props: {
|
||||||
|
type: 'text',
|
||||||
|
text: {
|
||||||
|
'$blocksuite:internal:text$': true,
|
||||||
|
delta: [
|
||||||
|
{
|
||||||
|
insert:
|
||||||
|
'prompt: How many people will live in the world in 2040?',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
children: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
workspaceId: '',
|
||||||
|
pageId: '',
|
||||||
|
};
|
||||||
|
|
||||||
|
const mdAdapter = new MarkdownAdapter(createJob(), provider);
|
||||||
|
const rawSliceSnapshot = await mdAdapter.toSliceSnapshot({
|
||||||
|
file: markdown,
|
||||||
|
workspaceId: '',
|
||||||
|
pageId: '',
|
||||||
|
});
|
||||||
|
expect(nanoidReplacement(rawSliceSnapshot!)).toEqual(sliceSnapshot);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import {
|
|||||||
type MarkdownAdapterPreprocessor,
|
type MarkdownAdapterPreprocessor,
|
||||||
MarkdownPreprocessorExtension,
|
MarkdownPreprocessorExtension,
|
||||||
} from '@blocksuite/affine-shared/adapters';
|
} from '@blocksuite/affine-shared/adapters';
|
||||||
|
import { isValidUrl } from '@blocksuite/affine-shared/utils';
|
||||||
|
|
||||||
const codePreprocessor: MarkdownAdapterPreprocessor = {
|
const codePreprocessor: MarkdownAdapterPreprocessor = {
|
||||||
name: 'code',
|
name: 'code',
|
||||||
@@ -53,14 +54,9 @@ const codePreprocessor: MarkdownAdapterPreprocessor = {
|
|||||||
//
|
//
|
||||||
// eg. /MuawcBMT1Mzvoar09-_66?mode=page&blockIds=rL2_GXbtLU2SsJVfCSmh_
|
// eg. /MuawcBMT1Mzvoar09-_66?mode=page&blockIds=rL2_GXbtLU2SsJVfCSmh_
|
||||||
// https://www.markdownguide.org/basic-syntax/#urls-and-email-addresses
|
// https://www.markdownguide.org/basic-syntax/#urls-and-email-addresses
|
||||||
try {
|
const valid = isValidUrl(trimmedLine);
|
||||||
const valid =
|
if (valid) {
|
||||||
URL.canParse?.(trimmedLine) ?? Boolean(new URL(trimmedLine));
|
return `<${trimmedLine}>`;
|
||||||
if (valid) {
|
|
||||||
return `<${trimmedLine}>`;
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -375,4 +375,26 @@ test.describe('paste to code block', () => {
|
|||||||
// Verify the pasted code maintains indentation
|
// Verify the pasted code maintains indentation
|
||||||
await verifyCodeBlockContent(page, 0, textWithHtmlTags);
|
await verifyCodeBlockContent(page, 0, textWithHtmlTags);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should not wrap line in brackets when pasting code', async ({
|
||||||
|
page,
|
||||||
|
}) => {
|
||||||
|
await pressEnter(page);
|
||||||
|
await addCodeBlock(page);
|
||||||
|
const plainTextCode = [
|
||||||
|
' model: anthropic("claude-3-7-sonnet-20250219"),',
|
||||||
|
' prompt: How many people will live in the world in 2040?',
|
||||||
|
' providerOptions: {',
|
||||||
|
' anthropic: {',
|
||||||
|
' thinking: { type: enabled, budgetTokens: 12000 },',
|
||||||
|
' } satisfies AnthropicProviderOptions,',
|
||||||
|
' },',
|
||||||
|
].join('\n');
|
||||||
|
|
||||||
|
await pasteContent(page, { 'text/plain': plainTextCode });
|
||||||
|
await page.waitForTimeout(100);
|
||||||
|
|
||||||
|
// Verify the pasted code maintains indentation
|
||||||
|
await verifyCodeBlockContent(page, 0, plainTextCode);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user