add empty node and Paper emoji unified plugins
This commit is contained in:
parent
a8fe57e5d6
commit
c49d84b2eb
@ -193,6 +193,7 @@ const RULES = [
|
|||||||
return {
|
return {
|
||||||
kind: 'inline',
|
kind: 'inline',
|
||||||
type: 'image',
|
type: 'image',
|
||||||
|
isVoid: true,
|
||||||
nodes: [],
|
nodes: [],
|
||||||
data: {
|
data: {
|
||||||
src: el.attribs.src,
|
src: el.attribs.src,
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import find from 'lodash/find';
|
||||||
import unified from 'unified';
|
import unified from 'unified';
|
||||||
import markdownToRemark from 'remark-parse';
|
import markdownToRemark from 'remark-parse';
|
||||||
import remarkToRehype from 'remark-rehype';
|
import remarkToRehype from 'remark-rehype';
|
||||||
@ -12,22 +13,61 @@ const remarkParseConfig = { fences: true };
|
|||||||
const remarkStringifyConfig = { listItemIndent: '1', fences: true };
|
const remarkStringifyConfig = { listItemIndent: '1', fences: true };
|
||||||
const rehypeParseConfig = { fragment: true };
|
const rehypeParseConfig = { fragment: true };
|
||||||
|
|
||||||
export const markdownToHtml = markdown =>
|
const rehypeRemoveEmpty = () => {
|
||||||
unified()
|
const isVoidElement = node => ['img', 'hr'].includes(node.tagName);
|
||||||
|
const isNonEmptyText = node => node.type === 'text' && node.value;
|
||||||
|
const isNonEmptyNode = node => {
|
||||||
|
return isVoidElement(node) || isNonEmptyText(node) || find(node.children, isNonEmptyNode);
|
||||||
|
};
|
||||||
|
|
||||||
|
const transform = node => {
|
||||||
|
if (isVoidElement(node) || isNonEmptyText(node)) {
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
if (node.children) {
|
||||||
|
node.children = node.children.reduce((acc, childNode) => {
|
||||||
|
if (isVoidElement(childNode) || isNonEmptyText(childNode)) {
|
||||||
|
return acc.concat(childNode);
|
||||||
|
}
|
||||||
|
return find(childNode.children, isNonEmptyNode) ? acc.concat(transform(childNode)) : acc;
|
||||||
|
}, []);
|
||||||
|
}
|
||||||
|
return node;
|
||||||
|
};
|
||||||
|
return transform;
|
||||||
|
};
|
||||||
|
|
||||||
|
const rehypePaperEmoji = () => {
|
||||||
|
const transform = node => {
|
||||||
|
if (node.tagName === 'img' && node.properties.dataEmojiCh) {
|
||||||
|
return { type: 'text', value: node.properties.dataEmojiCh };
|
||||||
|
}
|
||||||
|
node.children = node.children ? node.children.map(transform) : node.children;
|
||||||
|
return node;
|
||||||
|
};
|
||||||
|
return transform;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const markdownToHtml = markdown => {
|
||||||
|
console.log('markdownToHtml input', markdown);
|
||||||
|
const result = unified()
|
||||||
.use(markdownToRemark, remarkParseConfig)
|
.use(markdownToRemark, remarkParseConfig)
|
||||||
.use(remarkToRehype)
|
.use(remarkToRehype)
|
||||||
.use(rehypeSanitize)
|
|
||||||
.use(rehypeMinifyWhitespace)
|
|
||||||
.use(rehypeToHtml)
|
.use(rehypeToHtml)
|
||||||
.processSync(markdown)
|
.processSync(markdown)
|
||||||
.contents;
|
.contents;
|
||||||
|
console.log('markdownToHtml output', result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
export const htmlToMarkdown = html =>
|
export const htmlToMarkdown = html => {
|
||||||
unified()
|
console.log('htmlToMarkdown input', html);
|
||||||
|
const result = unified()
|
||||||
.use(htmlToRehype, rehypeParseConfig)
|
.use(htmlToRehype, rehypeParseConfig)
|
||||||
.use(rehypeSanitize)
|
|
||||||
.use(rehypeMinifyWhitespace)
|
|
||||||
.use(rehypeToRemark)
|
.use(rehypeToRemark)
|
||||||
.use(remarkToMarkdown, remarkStringifyConfig)
|
.use(remarkToMarkdown, remarkStringifyConfig)
|
||||||
.processSync(html)
|
.processSync(html)
|
||||||
.contents;
|
.contents;
|
||||||
|
console.log('htmlToMarkdown output', result);
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user