diff --git a/index.js b/index.js index bc0e991..c18b4f4 100644 --- a/index.js +++ b/index.js @@ -1,18 +1,32 @@ import fixGoogleHtml from './lib/fix-google-html'; // rehype-dom-parse is a lightweight version of rehype-parse that leverages // browser APIs -- reduces bundle size by ~200 kB! -// const parse = require('rehype-dom-parse').default; import parse from 'rehype-dom-parse'; +import { all } from 'rehype-remark'; import rehype2remarkWithSpaces from './lib/rehype-to-remark-with-spaces'; import stringify from 'remark-stringify'; import { unified } from 'unified'; +function preserveTagAndConvertContents (h, node) { + return [ + h(node, 'html', `<${node.tagName}>`), + ...all(h, node), + h(node, 'html', ``) + ]; +} + const processor = unified() .use(parse) .use(fixGoogleHtml) // .use(require('./lib/log-tree').default) - .use(rehype2remarkWithSpaces) + .use(rehype2remarkWithSpaces, { + handlers: { + // Preserve sup/sub markup; most Markdowns have no markup for it. + sub: preserveTagAndConvertContents, + sup: preserveTagAndConvertContents + } + }) .use(stringify, {listItemIndent: '1'}); function convertToMarkdown (html) { diff --git a/lib/fix-google-html.js b/lib/fix-google-html.js index 31b3d44..5eee10c 100644 --- a/lib/fix-google-html.js +++ b/lib/fix-google-html.js @@ -102,7 +102,7 @@ export function fixNestedLists (node) { /** * Google Docs does italics/bolds/etc on s with style attributes, but - * rehype-remark does pick up on those well. Instead, transform them into + * rehype-remark does not pick up on those. Instead, transform them into * `em`, `strong`, etc. elements. * * @param {RehypeNode} node Fix the tree below this node @@ -116,6 +116,11 @@ export function unInlineStyles (node) { if (/font-weight:\s*(bold|700)/.test(style)) { wrapChildren(node, hast('strong')); } + if (/vertical-align:\s*super/.test(style)) { + wrapChildren(node, hast('sup')); + } else if (/vertical-align:\s*sub/.test(style)) { + wrapChildren(node, hast('sub')); + } }); }