'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var LRUCache = require('lru-cache'); var path = require('path'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n['default'] = e; return Object.freeze(n); } var LRUCache__default = /*#__PURE__*/_interopDefaultLegacy(LRUCache); var path__namespace = /*#__PURE__*/_interopNamespace(path); const fakeHexToCSSVariable = (hex) => hex.replace(/^#FFFF(.+)/, 'var(--base$1)'); const cache = new LRUCache__default['default']({ max: 1024 * 1024 * 32, length(value, key) { return JSON.stringify(value).length + (key ? key.length : 0); }, }); /** * Adapted from * https://github.com/ggoodman/nostalgie/blob/45f3f6356684287a214dab667064ec9776def933/src/worker/mdxCompiler.ts * https://www.npmjs.com/package/@kentcdodds/md-temp * */ const remarkCodeBlockShiki = () => { return async (tree) => { const { htmlEscape } = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('escape-goat')); }); const shiki = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('shiki')); }); const unistUtil = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('unist-util-visit')); }); const theme = await shiki.loadTheme(path__namespace.resolve(__dirname, 'base16.json')); const themename = 'base16'; const highlighter = await shiki.getHighlighter({ theme }); unistUtil.visit(tree, 'code', (node) => { var _a, _b, _c; if (!node.value) return; const cacheKey = JSON.stringify([node.lang, node.value]); let nodeValue = cache.get(cacheKey); if (!nodeValue) { const fgColor = fakeHexToCSSVariable(highlighter.getForegroundColor(themename)); const bgColor = fakeHexToCSSVariable(highlighter.getBackgroundColor(themename)); const token = highlighter.codeToThemedTokens(node.value, (_a = node.lang) !== null && _a !== void 0 ? _a : 'sh', themename); const children = token.map((lineTokens, zeroBasedLineNumber) => { const children = lineTokens.map((token) => { var _a; const color = fakeHexToCSSVariable((_a = token.color) !== null && _a !== void 0 ? _a : ''); const content = { type: 'text', value: token.content, }; return color && color !== fgColor ? { type: 'element', tagName: 'span', properties: { style: `color: ${htmlEscape(color)}`, }, children: [content], } : content; }); children.push({ type: 'text', value: '\n', }); return { type: 'element', tagName: 'span', properties: { className: 'codeblock-line', dataLineNumber: zeroBasedLineNumber + 1, }, children, }; }); nodeValue = { type: 'element', tagName: 'pre', properties: { dataLang: htmlEscape(node.lang), style: `color: ${htmlEscape(fgColor)};background-color: ${htmlEscape(bgColor)}`, }, children: [ { type: 'element', tagName: 'code', children, }, ], }; cache.set(cacheKey, nodeValue); } const data = (_b = node.data) !== null && _b !== void 0 ? _b : (node.data = {}); // @ts-ignore node.type = 'element'; (_c = data.hProperties) !== null && _c !== void 0 ? _c : (data.hProperties = {}); data.hChildren = [nodeValue]; return unistUtil.SKIP; }); }; }; const remarkTOC = () => async (tree) => { const { toc } = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('mdast-util-toc')); }); const result = toc(tree); if (!result.map) { return; } tree.children = [result.map]; }; const rehypeRemovePreWrapper = () => async (tree) => { const { visit } = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('unist-util-visit')); }); visit(tree, { type: 'element', tagName: 'pre' }, (node, index, parent) => { if ((parent === null || parent === void 0 ? void 0 : parent.type) !== 'element') return; const parentEl = parent; if (parentEl.tagName !== 'div') return; if (parentEl.children.length !== 1 && index === 0) return; Object.assign(parentEl, node); }); }; exports.rehypeRemovePreWrapper = rehypeRemovePreWrapper; exports.remarkCodeBlockShiki = remarkCodeBlockShiki; exports.remarkTOC = remarkTOC;