This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Koenig - Convert <br>s to soft-break atoms on paste
refs TryGhost/Ghost#9623 - adds `parserPlugins` option with array of parser plugins that read node values and convert them when pasting - converts `<br>` to a soft-break atom for line breaks - removes leading newlines from text nodes to avoid leading spaces in the render output (common when pasting MD with line breaks)
- Loading branch information
1 parent
faba3f5
commit 4370f33
Showing
2 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// mobiledoc by default ignores <BR> tags but we have a custom SoftReturn atom | ||
export function brToSoftBreakAtom(node, builder, {addMarkerable, nodeFinished}) { | ||
if (node.nodeType !== 1 || node.tagName !== 'BR') { | ||
return; | ||
} | ||
|
||
let softReturn = builder.createAtom('soft-return'); | ||
addMarkerable(softReturn); | ||
|
||
nodeFinished(); | ||
} | ||
|
||
// leading newlines in text nodes will add a space to the beginning of the text | ||
// which doesn't render correctly if we're replacing <br> with SoftReturn atoms | ||
// after parsing text as markdown to html | ||
export function removeLeadingNewline(node) { | ||
if (node.nodeType !== 3 || node.nodeName !== '#text') { | ||
return; | ||
} | ||
|
||
node.nodeValue = node.nodeValue.replace(/^\n/, ''); | ||
} | ||
|
||
export default [ | ||
brToSoftBreakAtom, | ||
removeLeadingNewline, | ||
]; |