Improved paste handling (#4474)

* Improved paste handling

* Remove file
This commit is contained in:
Tom Moor
2022-11-24 06:50:31 -08:00
committed by GitHub
parent a6125be6f1
commit a8936039e5
4 changed files with 59 additions and 26 deletions

View File

@@ -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;
}

View 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;
}

View File

@@ -4,6 +4,7 @@ import { isInTable } from "prosemirror-tables";
import { isUrl } from "../../utils/urls";
import Extension from "../lib/Extension";
import isMarkdown from "../lib/isMarkdown";
import normalizePastedMarkdown from "../lib/markdown/normalize";
import isInCode from "../queries/isInCode";
import { LANGUAGES } from "./Prism";
@@ -13,29 +14,6 @@ function isDropboxPaper(html: string): boolean {
return html?.includes("usually-unique-id");
}
/**
* 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
*/
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{2,}/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;
}
export default class PasteHandler extends Extension {
get name() {
return "markdown-paste";