UNPKG

@vivliostyle/vfm

Version:

Custom Markdown syntax specialized in book authoring.

41 lines (40 loc) 1.19 kB
import { all } from 'mdast-util-to-hast'; import { u } from 'unist-builder'; // remark function locateRuby(value, fromIndex) { return value.indexOf('{', fromIndex); } const tokenizer = function (eat, value, silent) { const now = eat.now(); const match = /^{(.+?)(?<=[^\\|])\|(.+?)}/.exec(value); if (!match) return; const [eaten, inlineContent, rubyText] = match; if (silent) return true; now.column += 1; now.offset += 1; return eat(eaten)({ type: 'ruby', children: this.tokenizeInline(inlineContent, now), data: { hName: 'ruby', rubyText }, }); }; tokenizer.notInLink = true; tokenizer.locator = locateRuby; export const mdast = function () { if (!this.Parser) return; const { inlineTokenizers, inlineMethods } = this.Parser.prototype; inlineTokenizers.ruby = tokenizer; inlineMethods.splice(inlineMethods.indexOf('text'), 0, 'ruby'); }; // rehype export const handler = (h, node) => { if (!node.data) node.data = {}; const rtNode = h({ type: 'element', }, 'rt', [u('text', node.data.rubyText)]); return h(node, 'ruby', [...all(h, node), rtNode]); };