UNPKG

@vivliostyle/vfm

Version:

Custom Markdown syntax specialized in book authoring.

58 lines (57 loc) 2.07 kB
"use strict"; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.handler = exports.mdast = void 0; var all_1 = __importDefault(require("mdast-util-to-hast/lib/all")); var unist_builder_1 = __importDefault(require("unist-builder")); // remark function locateRuby(value, fromIndex) { return value.indexOf('{', fromIndex); } var tokenizer = function (eat, value, silent) { var now = eat.now(); var match = /^{(.+?)(?<=[^\\|])\|(.+?)}/.exec(value); if (!match) return; var eaten = match[0], inlineContent = match[1], rubyText = match[2]; if (silent) return true; now.column += 1; now.offset += 1; return eat(eaten)({ type: 'ruby', children: this.tokenizeInline(inlineContent, now), data: { hName: 'ruby', rubyText: rubyText }, }); }; tokenizer.notInLink = true; tokenizer.locator = locateRuby; var mdast = function () { if (!this.Parser) return; var _a = this.Parser.prototype, inlineTokenizers = _a.inlineTokenizers, inlineMethods = _a.inlineMethods; inlineTokenizers.ruby = tokenizer; inlineMethods.splice(inlineMethods.indexOf('text'), 0, 'ruby'); }; exports.mdast = mdast; // rehype var handler = function (h, node) { if (!node.data) node.data = {}; var rtNode = h({ type: 'element', }, 'rt', [(0, unist_builder_1.default)('text', node.data.rubyText)]); return h(node, 'ruby', __spreadArray(__spreadArray([], (0, all_1.default)(h, node), true), [rtNode], false)); }; exports.handler = handler;