New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
paste-md plugin added #74
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { jsx } from 'slate-hyperscript'; | ||
|
||
export const ELEMENT_TAGS: any = { | ||
A: (el: any) => ({ 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: any) => ({ 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. | ||
export const TEXT_TAGS: any = { | ||
CODE: () => ({ code: true }), | ||
DEL: () => ({ strikethrough: true }), | ||
EM: () => ({ italic: true }), | ||
I: () => ({ italic: true }), | ||
S: () => ({ strikethrough: true }), | ||
STRONG: () => ({ bold: true }), | ||
U: () => ({ underline: true }), | ||
}; | ||
|
||
export function deserializeMd(el: any) { | ||
if (el.nodeType === 3) { | ||
return el.textContent; | ||
} | ||
if (el.nodeType !== 1) { | ||
return null; | ||
} | ||
if (el.nodeName === 'BR') { | ||
return '\n'; | ||
} | ||
|
||
const { nodeName } = el; | ||
let parent = el; | ||
|
||
if ( | ||
nodeName === 'PRE' && | ||
el.childNodes[0] && | ||
el.childNodes[0].nodeName === 'CODE' | ||
) { | ||
const [p] = el.childNodes; | ||
parent = p; | ||
} | ||
|
||
// TODO: fix types | ||
const children: Array<any> = Array.from(parent.childNodes) | ||
.map(deserializeMd) | ||
.flat(); | ||
|
||
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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./withPasteMd"; | ||
export * from "./deserializeMd"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import marked from "marked"; | ||
import { Node, Transforms } from "slate"; | ||
import { ReactEditor } from "slate-react"; | ||
import { deserializeMd } from "./deserializeMd"; | ||
|
||
export function filterBreaklines(item: any): boolean { | ||
return !item.text; | ||
} | ||
|
||
export const withPasteMd = <T extends ReactEditor>(editor: T) => { | ||
const { insertData, isInline, isVoid } = editor; | ||
|
||
editor.isInline = element => { | ||
return element.type === "link" ? true : isInline(element); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use the constant type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep! |
||
}; | ||
|
||
editor.isVoid = element => { | ||
return element.type === "image" ? true : isVoid(element); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use the constant type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep! |
||
}; | ||
|
||
editor.insertData = data => { | ||
const content = data.getData("text/plain"); | ||
|
||
if (content) { | ||
const html = marked(content); | ||
const parsed = new DOMParser().parseFromString(html, "text/html"); | ||
|
||
// `filterBreaklines` filters all the breaklines in the pasted document | ||
const fragment: Array<Node> = deserializeMd(parsed.body).filter( | ||
filterBreaklines | ||
); | ||
|
||
Transforms.insertFragment(editor, fragment); | ||
return; | ||
} | ||
|
||
insertData(data); | ||
}; | ||
|
||
return editor; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use the constant types? e.g.
LINK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
of course! gonna check the constant types available :)