Skip to content
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

Merged
merged 1 commit into from Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/slate-plugins/package.json
Expand Up @@ -29,6 +29,7 @@
"image-extensions": "^1.1.0",
"is-hotkey": "^0.1.6",
"is-url": "^1.2.4",
"marked": "^0.8.0",
"prismjs": "^1.17.1"
},
"peerDependencies": {
Expand All @@ -38,5 +39,8 @@
"slate-hyperscript": "^0.57.1",
"slate-react": "^0.57.1",
"styled-components": "^4.4.0"
},
"devDependencies": {
"@types/marked": "^0.7.2"
}
}
1 change: 1 addition & 0 deletions packages/slate-plugins/src/index.ts
Expand Up @@ -6,6 +6,7 @@ export * from './markdown-preview';
export * from './markdown-shortcuts';
export * from './marks';
export * from './paste-html';
export * from './paste-md';
export * from './search-highlight';
export * from './toolbar';
export * from './types';
74 changes: 74 additions & 0 deletions packages/slate-plugins/src/paste-md/deserializeMd.ts
@@ -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' }),
Comment on lines +4 to +17
Copy link
Member

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

Copy link
Collaborator Author

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 :)

};

// 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;
}
2 changes: 2 additions & 0 deletions packages/slate-plugins/src/paste-md/index.ts
@@ -0,0 +1,2 @@
export * from "./withPasteMd";
export * from "./deserializeMd";
41 changes: 41 additions & 0 deletions packages/slate-plugins/src/paste-md/withPasteMd.ts
@@ -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);
Copy link
Member

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 type LINK?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep!

};

editor.isVoid = element => {
return element.type === "image" ? true : isVoid(element);
Copy link
Member

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 type IMAGE?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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