UNPKG

@meldscience/meld

Version:

pipeable one-shot prompt scripting toolkit

49 lines (43 loc) 1.54 kB
import { Plugin } from 'unified'; import { MeldDirectiveNode } from './remarkMeldDirectives'; import { toMarkdown } from 'mdast-util-to-markdown'; interface PluginData { [key: string]: unknown[]; } export const remarkMeldDirectiveHandler: Plugin = function remarkMeldDirectiveHandler() { const self = this; const data = self.data() as PluginData; // Add a handler for meldDirective nodes const handle = (node: MeldDirectiveNode) => { if (node.meldType === 'cmd' && node.data?.command) { return `@cmd[${node.data.command}]`; } else if (node.meldType === 'import' && node.data) { const { importPath, heading, importSymbol, importSymbolList } = node.data; let specifier = ''; if (heading) { specifier = ` # ${heading}`; } else if (importSymbolList) { if (importSymbolList.length === 1 && importSymbolList[0].startsWith('{')) { specifier = ` # ${importSymbolList[0]}`; } else { specifier = ` # [${importSymbolList.join(', ')}]`; } } else if (importSymbol) { specifier = ` # ${importSymbol}`; } return importPath ? `@import[${importPath}${specifier}]` : ''; } return ''; }; // Register the handler with mdast-util-to-markdown const toMarkdownExtension = { handlers: { meldDirective: handle } }; add('toMarkdownExtensions', toMarkdownExtension); function add(field: string, value: unknown) { const list = data[field] ? data[field] : (data[field] = []); list.push(value); } }