UNPKG

@docfy/ember-vite

Version:

Vite plugin for Docfy Ember integration with @embroider/vite

40 lines (39 loc) 1.33 kB
import plugin from '@docfy/core/lib/plugin'; import visit from 'unist-util-visit'; import u from 'unist-builder'; function visitor(page) { visit(page.ast, 'link', (node, index, parent) => { if (node.url[0] === '/') { const data = node.data || (node.data = {}); const props = (data.hProperties || (data.hProperties = {})); const urlParts = node.url.split('#'); const attributes = Object.keys(props) .map((key) => { return `${key}=${String(props[key])}`; }) .join(' '); const toInsert = [ u('html', `<DocfyLink @to="${urlParts[0]}" ${urlParts[1] ? `@anchor="${urlParts[1]}"` : ''} ${attributes}>`), ...node.children, u('html', `</DocfyLink>`) ]; parent?.children.splice(index, 1, ...toInsert); } }); } /** * This function finds all the links starting with an `/` and replace them with * the `DocfyLink` component. */ export default plugin({ runWithMdast(ctx) { ctx.pages.forEach((page) => { visitor(page); if (Array.isArray(page.demos)) { page.demos.forEach((demo) => { visitor(demo); }); } }); } });