-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(markdown): copying html into markdown (#7290)
* fix(markdown): copying html into markdown //issues/7233 * refactor: sync markdown html parsing script with original * fix: lint --------- Co-authored-by: Anze Demsar <anze.demsar@p-m.si>
- Loading branch information
Showing
4 changed files
with
105 additions
and
4 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
98 changes: 98 additions & 0 deletions
98
packages/decap-cms-widget-markdown/src/MarkdownControl/plugins/html/withHtml.js
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,98 @@ | ||
// source: https://github.com/ianstormtaylor/slate/blob/main/site/examples/ts/paste-html.tsx | ||
import { jsx } from 'slate-hyperscript'; | ||
import { Transforms } from 'slate'; | ||
|
||
const ELEMENT_TAGS = { | ||
A: el => ({ type: 'link', url: el.getAttribute('href') }), | ||
BLOCKQUOTE: () => ({ type: 'quote' }), | ||
H1: () => ({ type: 'heading-one' }), | ||
H2: () => ({ type: 'heading-two' }), | ||
H3: () => ({ type: 'heading-three' }), | ||
H4: () => ({ type: 'heading-four' }), | ||
H5: () => ({ type: 'heading-five' }), | ||
H6: () => ({ type: 'heading-six' }), | ||
IMG: el => ({ type: 'image', url: el.getAttribute('src') }), | ||
LI: () => ({ type: 'list-item' }), | ||
OL: () => ({ type: 'numbered-list' }), | ||
P: () => ({ type: 'paragraph' }), | ||
PRE: () => ({ type: 'code' }), | ||
UL: () => ({ type: 'bulleted-list' }), | ||
}; | ||
|
||
// COMPAT: `B` is omitted here because Google Docs uses `<b>` in weird ways. | ||
const TEXT_TAGS = { | ||
CODE: () => ({ code: true }), | ||
DEL: () => ({ strikethrough: true }), | ||
EM: () => ({ italic: true }), | ||
I: () => ({ italic: true }), | ||
S: () => ({ strikethrough: true }), | ||
STRONG: () => ({ bold: true }), | ||
U: () => ({ underline: true }), | ||
}; | ||
|
||
function deserialize(el) { | ||
if (el.nodeType === 3) { | ||
return el.textContent; | ||
} else if (el.nodeType !== 1) { | ||
return null; | ||
} else if (el.nodeName === 'BR') { | ||
return '\n'; | ||
} | ||
|
||
const { nodeName } = el; | ||
let parent = el; | ||
|
||
if (nodeName === 'PRE' && el.childNodes[0] && el.childNodes[0].nodeName === 'CODE') { | ||
parent = el.childNodes[0]; | ||
} | ||
let children = Array.from(parent.childNodes).map(deserialize).flat(); | ||
|
||
if (children.length === 0) { | ||
children = [{ text: '' }]; | ||
} | ||
|
||
if (el.nodeName === 'BODY') { | ||
return jsx('fragment', {}, children); | ||
} | ||
|
||
if (ELEMENT_TAGS[nodeName]) { | ||
const attrs = ELEMENT_TAGS[nodeName](el); | ||
return jsx('element', attrs, children); | ||
} | ||
|
||
if (TEXT_TAGS[nodeName]) { | ||
const attrs = TEXT_TAGS[nodeName](el); | ||
return children.map(child => jsx('text', attrs, child)); | ||
} | ||
|
||
return children; | ||
} | ||
|
||
function withHtml(editor) { | ||
const { insertData, isInline, isVoid } = editor; | ||
|
||
editor.isInline = element => { | ||
return element.type === 'link' ? true : isInline(element); | ||
}; | ||
|
||
editor.isVoid = element => { | ||
return element.type === 'image' ? true : isVoid(element); | ||
}; | ||
|
||
editor.insertData = data => { | ||
const html = data.getData('text/html'); | ||
|
||
if (html) { | ||
const parsed = new DOMParser().parseFromString(html, 'text/html'); | ||
const fragment = deserialize(parsed.body); | ||
Transforms.insertFragment(editor, fragment); | ||
return; | ||
} | ||
|
||
insertData(data); | ||
}; | ||
|
||
return editor; | ||
} | ||
|
||
export default withHtml; |
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