|
Dear markedjs community, my custom renderer to strip markdown, throws a I'm trying to find the proper solution for this but can't figure it out. My goal is to strip all markdown formatting from the input. I'd greatly appreciate some tips on how to solve this. Not sure if this is the right approach for stripping markdown anyways. import { Marked, type RendererObject } from 'marked'
const plainTextRenderer: RendererObject = {
hr: () => '',
checkbox: () => '',
code: ({ text }) => text,
blockquote: ({ text }) => text,
heading(this, { tokens }) {
return this.parser.parseInline(tokens) + '\n'
},
list(this, { items, ordered }) {
const texts = items.map((input) => this.parser.parseInline(input.tokens))
if (ordered) {
return texts.map((text, i) => `${i + 1}. ${text}`).join('\n')
}
return texts.map((text) => `- ${text}`).join('\n')
},
listitem(this, { tokens }) {
return this.parser.parseInline(tokens)
},
paragraph(this, { tokens }) {
return this.parser.parseInline(tokens)
},
table(this, { header, rows }) {
const headerStr = header.map(({ tokens }) => this.parser.parseInline(tokens)).join(', ')
const rowsStr = rows
.map((row) => row.map(({ tokens }) => this.parser.parseInline(tokens)).join(', '))
.join('\n')
return `\n${headerStr}\n${rowsStr}\n\n`
},
tablecell(this, { tokens }) {
return this.parser.parseInline(tokens)
},
// Inline Tokens
strong(this, { tokens }) {
return this.parser.parseInline(tokens)
},
em(this, { tokens }) {
return this.parser.parseInline(tokens)
},
del(this, { tokens }) {
return this.parser.parseInline(tokens)
},
codespan: ({ text }) => text,
html: ({ text }) => text,
link(this, { title, href, tokens }) {
const text = this.parser.parseInline(tokens)
return `Link: ${text ?? title ?? ''} (${href})`
},
image: ({ title, text }) => `Bild: ${title ? `${title}, ` : ''} ${text ?? 'Keine Beschreibung'}`,
tablerow: ({ text }) => text + '\n',
text: ({ text }) => text,
space: ({ raw }) => raw,
br: () => '\n',
}
const marked = new Marked({ renderer: plainTextRenderer })
/**
* Strips markdown from a string.
*/
export function stripMarkdown(text: string) {
return marked.parse(text, { async: false })
}
const test = `
* bla
* bla
`
console.log(stripMarkdown(test)) |
Answered by
UziTech
Dec 10, 2024
Replies: 1 comment 2 replies
|
List is a block level token that can contain other block level tokens but you use list(this, { items, ordered }) {
const texts = items.map((input) => this.parser.parse(input.tokens)) |
2 replies
Answer selected by
maltesa
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
List is a block level token that can contain other block level tokens but you use
parseInlineto render it's children. Change it toparseand it should work.