UNPKG

markdown-code-example-inserter

Version:
23 lines (22 loc) 997 B
const markdownCodeBlockWrapper = '```'; export function insertCodeExample(markdownText, language, fixedCode, linkComment) { const markdownCodeBlock = `${linkComment.indent}${markdownCodeBlockWrapper}${language ?? ''}\n${fixedCode}\n${linkComment.indent}${markdownCodeBlockWrapper}`; if (linkComment.linkedCodeBlock) { return replaceTextRange(markdownText, [ linkComment.linkedCodeBlock.position.start.offset - linkComment.indent.length, linkComment.linkedCodeBlock.position.end.offset, ], markdownCodeBlock); } else { return insertText(markdownText, linkComment.node.position.end.offset, `\n\n${markdownCodeBlock}`); } } export function insertText(text, insertAtThisIndex, insertion) { return replaceTextRange(text, [ insertAtThisIndex, insertAtThisIndex, ], insertion); } export function replaceTextRange(text, range, insertion) { return text.slice(0, range[0]) + insertion + text.slice(range[1]); }