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

fix(markdown): copying html into markdown #7290

Merged
merged 4 commits into from
Nov 12, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { markdownToSlate, slateToMarkdown } from '../serializers';
import withShortcodes from './plugins/shortcodes/withShortcodes';
import insertShortcode from './plugins/shortcodes/insertShortcode';
import defaultEmptyBlock from './plugins/blocks/defaultEmptyBlock';
import withHtml from './plugins/html/withHtml';

function visualEditorStyles({ minimal }) {
return `
Expand Down Expand Up @@ -97,7 +98,9 @@ function Editor(props) {

const editor = useMemo(
() =>
withReact(withHistory(withShortcodes(withBlocks(withLists(withInlines(createEditor())))))),
withHtml(
withReact(withHistory(withShortcodes(withBlocks(withLists(withInlines(createEditor())))))),
),
[],
);

Expand Down
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.

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

great catch.
Slate also noticed and updated this, so I just copied new version of example.

Should the comment, or the B mapping be removed?

Since it's known that Google Docs uses <b> differently, I would leave it removed as is

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;
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ function NumberedList(props) {
}

function Link(props) {
const url = props.url;
const title = props.title || url;
const url = props.element.url;
const title = props.element.title || url;

return (
<StyledA href={url} title={title} {...props.attributes}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ export default function slateToRemark(value, { voidCodeBlock }) {
*/
case 'link': {
const { title, data } = node;
return u(typeMap[node.type], { url: data?.url, title, ...data }, children);
return u(typeMap[node.type], { url: node.url, title, ...data }, children);
}

/**
Expand Down
Loading