Improved paste handling (#4474)
* Improved paste handling * Remove file
This commit is contained in:
@@ -19,7 +19,7 @@ export default function isMarkdown(text: string): boolean {
|
||||
}
|
||||
|
||||
// list-ish
|
||||
const listItems = text.match(/^[\d-*].?\s\S+/gm);
|
||||
const listItems = text.match(/^([-*]|\d+.)\s\S+/gm);
|
||||
if (listItems && listItems.length > 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
22
shared/editor/lib/markdown/normalize.ts
Normal file
22
shared/editor/lib/markdown/normalize.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Add support for additional syntax that users paste even though it isn't
|
||||
* supported by the markdown parser directly by massaging the text content.
|
||||
*
|
||||
* @param text The incoming pasted plain text
|
||||
*/
|
||||
export default function normalizePastedMarkdown(text: string): string {
|
||||
const CHECKBOX_REGEX = /^\s?(\[(X|\s|_|-)\]\s(.*)?)/gim;
|
||||
|
||||
// find checkboxes not contained in a list and wrap them in list items
|
||||
while (text.match(CHECKBOX_REGEX)) {
|
||||
text = text.replace(CHECKBOX_REGEX, (match) => `- ${match.trim()}`);
|
||||
}
|
||||
|
||||
// find multiple newlines and insert a hard break to ensure they are respected
|
||||
text = text.replace(/\n{3,}/g, "\n\n\\\n");
|
||||
|
||||
// find single newlines and insert an extra to ensure they are treated as paragraphs
|
||||
text = text.replace(/\b\n\b/g, "\n\n");
|
||||
|
||||
return text;
|
||||
}
|
||||
Reference in New Issue
Block a user