Strip code fences from markdown rendering in terminal output
This commit is contained in:
parent
7c5746e268
commit
3e423a3b37
|
|
@ -1,7 +1,10 @@
|
||||||
export function renderMarkdown(text: string): string {
|
export function renderMarkdown(text: string): string {
|
||||||
|
// Strip code fences (``` and ```language) — we're already in a terminal
|
||||||
|
let result = text.replace(/^```\w*\n?/gm, "")
|
||||||
|
|
||||||
// Extract code spans first so their contents aren't processed as bold/italic
|
// Extract code spans first so their contents aren't processed as bold/italic
|
||||||
const codeSpans: string[] = []
|
const codeSpans: string[] = []
|
||||||
let result = text.replace(/`([^`]+)`/g, (_, code) => {
|
result = result.replace(/`([^`]+)`/g, (_, code) => {
|
||||||
codeSpans.push(code)
|
codeSpans.push(code)
|
||||||
return `\x00CODE${codeSpans.length - 1}\x00`
|
return `\x00CODE${codeSpans.length - 1}\x00`
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -16,3 +16,19 @@ for (const line of lines) {
|
||||||
console.log(` ${renderMarkdown(line)}`)
|
console.log(` ${renderMarkdown(line)}`)
|
||||||
console.log()
|
console.log()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Code fence tests
|
||||||
|
console.log("--- Code fence tests ---")
|
||||||
|
const fenceTests = [
|
||||||
|
"Here is some code:\n```typescript\nconst x = 1\n```\nDone.",
|
||||||
|
"```\nhello\n```",
|
||||||
|
"```js\nconsole.log('hi')\n```",
|
||||||
|
]
|
||||||
|
|
||||||
|
for (const test of fenceTests) {
|
||||||
|
console.log("INPUT:")
|
||||||
|
console.log(test)
|
||||||
|
console.log("OUTPUT:")
|
||||||
|
console.log(renderMarkdown(test))
|
||||||
|
console.log()
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user