Skip to content

Commit

Permalink
Support superscript and subscript (#34)
Browse files Browse the repository at this point in the history
You can have superscript and subscript text in Google Docs, but this app would previously remove the formatting. We now keep it in place, even if it outputs unfortunately verbose markup: most Markdown flavors have no markup for superscript or subscript, so we output HTML tags, e.g:

    This<sub>is subscripted</sub> and this<sup>is superscripted</sup> text.

This also updates most of our Rehype and Unified dependencies (they offer some new utilities and fixes that make this feature easier to implement). I did *not* update Remark here, though, since it has some major changes in v8 that need more careful review (really, I should have some tests).

Fixes #33.
  • Loading branch information
Mr0grog authored Apr 18, 2022
1 parent 8fdaadd commit ef0a7a2
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 289 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
4 changes: 2 additions & 2 deletions lib/rehype-to-remark-with-spaces.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const rehype2remark = require('rehype-remark');
import rehypeRemark from 'rehype-remark';

/**
* The official rehype-remark plugin gets a little aggeressive with removing
Expand Down Expand Up @@ -33,7 +33,7 @@ export default function rehype2remarkWithSpaces () {
}
}

const convert = rehype2remark.apply(this, arguments);
const convert = rehypeRemark.apply(this, arguments);
return function (tree, file) {
preserveInitialSpaces(tree);
const markdownTree = convert.apply(this, [tree, file]);
Expand Down
Loading

0 comments on commit ef0a7a2

Please sign in to comment.