init
This commit is contained in:
37
src/web/lib/markdown.ts
Normal file
37
src/web/lib/markdown.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { marked } from 'marked'
|
||||
import hljs from 'highlight.js'
|
||||
|
||||
// Configure marked
|
||||
marked.setOptions({
|
||||
breaks: true,
|
||||
gfm: true,
|
||||
})
|
||||
|
||||
// Custom renderer for syntax highlighting
|
||||
const renderer = new marked.Renderer()
|
||||
|
||||
renderer.code = function({ text, lang }: { text: string; lang?: string }) {
|
||||
if (lang && hljs.getLanguage(lang)) {
|
||||
const highlighted = hljs.highlight(text, { language: lang }).value
|
||||
return `<pre><code class="hljs language-${lang}">${highlighted}</code></pre>`
|
||||
}
|
||||
const escaped = escapeHtml(text)
|
||||
return `<pre><code>${escaped}</code></pre>`
|
||||
}
|
||||
|
||||
marked.use({ renderer })
|
||||
|
||||
// Escape HTML
|
||||
function escapeHtml(text: string): string {
|
||||
return text
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''')
|
||||
}
|
||||
|
||||
// Render markdown to HTML
|
||||
export function renderMarkdown(content: string): string {
|
||||
return marked(content) as string
|
||||
}
|
||||
Reference in New Issue
Block a user