Skip to content

Commit

Permalink
Support superscript/subscript, fixes #33
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr0grog committed Apr 18, 2022
1 parent 1b2240c commit fe62020
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
18 changes: 16 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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', `</${node.tagName}>`)
];
}

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) {
Expand Down
7 changes: 6 additions & 1 deletion lib/fix-google-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function fixNestedLists (node) {

/**
* Google Docs does italics/bolds/etc on <span>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
Expand All @@ -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'));
}
});
}

Expand Down

0 comments on commit fe62020

Please # to comment.