Markdown
Zero-dependency markdown renderer. Parses headings, bold, italic, inline code, fenced blocks, lists, and links.
Use and inspectUse this as a presentational interface around your own submit, stream, or state handlers. Reset preview only remounts the local example; it does not send a request or connect to a model.Next: inspect the source and wire its callback to a bounded backend action.
MarkdownZero-dependency markdown renderer. Parses headings, bold, italic, inline code, fenced blocks, lists, and links.
type MarkdownProps = {
content: string;
};
function parseMarkdown(content: string): string {
const lines = content.split("\n");
let inCodeBlock = false;
const codeLines: string[] = [];
const result: string[] = [];
for (const line of lines) {
if (line.startsWith("```")) {
if (!inCodeBlock) {
inCodeBlock = true;
codeLines.length = 0;
} else {
inCodeBlock = false;
const escaped = codeLines
.join("\n")
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
result.push('<pre class="pk-md-code-block"><code>' + escaped + "</code></pre>");
}
continue;
}
if (inCodeBlock) {
codeLines.push(line);
continue;
}
let html = line;
if (html.startsWith("### ")) {
html = '<h3 class="pk-md-h3">' + html.slice(4) + "</h3>";
} else if (html.startsWith("## ")) {
html = '<h2 class="pk-md-h2">' + html.slice(3) + "</h2>";
} else if (html.startsWith("# ")) {
html = '<h1 class="pk-md-h1">' + html.slice(2) + "</h1>";
} else if (html.startsWith("- ")) {
const inner = applyInline(html.slice(2));
html = '<li class="pk-md-li">' + inner + "</li>";
} else if (html.trim() === "") {
html = "<br/>";
} else {
html = '<p class="pk-md-p">' + applyInline(html) + "</p>";
}
result.push(html);
}
return result.join("\n");
}
function applyInline(s: string): string {
return s
.replace(/\*\*(.*?)\*\*/g, "<strong>$1</strong>")
.replace(/\*(.*?)\*/g, "<em>$1</em>")
.replace(/`([^`]+)`/g, '<code class="pk-md-code">$1</code>')
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2" class="pk-md-link" target="_blank" rel="noopener noreferrer">$1</a>');
}
export function Markdown({ content }: MarkdownProps) {
return (
<div
className="pk-md"
dangerouslySetInnerHTML={{ __html: parseMarkdown(content) }}
/>
);
}Props
| Prop | Type | Default | Description |
|---|---|---|---|
content* | string | — | Markdown string to render |