import { PluginTexts, type ExpressiveCodePlugin } from '@expressive-code/core'; import { pluginCodeBlockButton } from './base-plugin'; const svg = [ ``, ``, ``, ].join(''); export const runInTerminalTexts = new PluginTexts({ buttonTooltip: 'Run in terminal', buttonExecuted: 'Command executing...', }); export function runInTerminalPlugin(): ExpressiveCodePlugin { return pluginCodeBlockButton( 'runInTerminal', svg, runInTerminalTexts, (_, isTerminal) => isTerminal, (codeBlock, _) => { // remove comment lines starting with `#` from terminal frames let code = codeBlock.code.replace(/(?<=^|\n)\s*#.*($|\n+)/g, '').trim(); /** * Replace all line breaks with a special character * because HAST does not encode them in attribute values * (which seems to work, but looks ugly in the HTML source) */ code = code.replace(/\n/g, '\u007f'); return { 'data-code': code, }; } ); }