tessl install tessl/npm-marked@17.0.0A markdown parser built for speed
Advanced scenarios and edge cases.
import { marked } from "marked";
marked.parse(''); // Returns: ''
marked.parseInline(''); // Returns: ''import { marked } from "marked";
try {
marked.parse(null); // Throws: TypeError
marked.parse(undefined); // Throws: TypeError
} catch (err) {
console.error('Invalid input:', err.message);
}import { marked } from "marked";
// Very large inputs (>10MB) may have performance implications
const largeMarkdown = '...'; // 10MB+
console.time('parse');
const html = marked.parse(largeMarkdown);
console.timeEnd('parse');
// Typical: < 1 second for 10MBimport { marked } from "marked";
function parseInChunks(markdown, chunkSize = 100000) {
const chunks = [];
const paragraphs = markdown.split(/\n\n+/);
let currentChunk = [];
let currentSize = 0;
for (const para of paragraphs) {
if (currentSize + para.length > chunkSize && currentChunk.length > 0) {
chunks.push(currentChunk.join('\n\n'));
currentChunk = [];
currentSize = 0;
}
currentChunk.push(para);
currentSize += para.length;
}
if (currentChunk.length > 0) {
chunks.push(currentChunk.join('\n\n'));
}
return chunks.map(chunk => marked.parse(chunk)).join('\n');
}import { marked } from "marked";
// Links cannot contain other links (per markdown spec)
const html = marked.parse('[Outer [nested](url)](url)');
// Inner link is treated as textExplanation: The lexer's inLink state prevents nested links per markdown specification.
import { marked } from "marked";
const nested = '> '.repeat(100) + 'text';
const html = marked.parse(nested);
// Handles deep nestingimport { marked } from "marked";
const markdown = `
- Level 1
- Level 2
- Level 3
- Level 4
- Level 5
`;
const html = marked.parse(markdown);
// Properly handles nested listsimport { marked } from "marked";
const html = marked.parse('\\* Not a list\n\\# Not a heading');
// Escaped characters rendered literallyimport { marked } from "marked";
const html = marked.parse('# 你好世界 🌍\n\nEmoji and Unicode: 😀 ñ é ü');
// Full Unicode supportimport { marked } from "marked";
marked.setOptions({ gfm: true });
const html = marked.parse(`
- [ ] Unchecked task
- [x] Checked task
- [ ] Another task
`);import { marked } from "marked";
marked.setOptions({ gfm: true });
const html = marked.parse(`
| Left | Center | Right |
|:-----|:------:|------:|
| L | C | R |
`);import { marked } from "marked";
marked.setOptions({ gfm: true });
const html = marked.parse('~~deleted text~~');
// Output: <p><del>deleted text</del></p>import { marked } from "marked";
marked.setOptions({ gfm: true });
const html = marked.parse('Visit https://example.com for more.');
// Raw URLs become clickable linksimport { marked } from "marked";
marked.setOptions({ breaks: false });
const html = marked.parse('Line 1\nLine 2');
// Output: <p>Line 1\nLine 2</p> (no <br>)
const html2 = marked.parse('Line 1 \nLine 2');
// Output: <p>Line 1<br>Line 2</p> (two spaces + newline)import { marked } from "marked";
marked.setOptions({ gfm: true, breaks: true });
const html = marked.parse('Line 1\nLine 2');
// Output: <p>Line 1<br>Line 2</p>import { marked } from "marked";
const html = marked.parse(`
<div class="custom">
Custom HTML block
</div>
Regular markdown continues here.
`);
// HTML passed through unchangedimport { marked } from "marked";
const html = marked.parse('Text with <span class="custom">inline HTML</span>.');
// Inline HTML passed throughimport { marked } from "marked";
const markdown = `
[Google]: https://google.com "Google Search"
[GitHub]: https://github.com
Visit [Google] or [GitHub].
`;
const html = marked.parse(markdown);
// References resolved to full linksimport { marked } from "marked";
const markdown = `
[Google]: https://google.com
Visit [Google].
`;
const html = marked.parse(markdown);import { marked } from "marked";
const markdown = `
\`\`\`javascript
console.log('code');
\`\`\`
`;
const html = marked.parse(markdown);import { marked } from "marked";
const markdown = `
indented code
more code
`;
const html = marked.parse(markdown);
// 4+ spaces creates code blockimport { marked } from "marked";
const html = marked.parseInline('`` code with ` backtick ``');
// Multiple backticks for code containing backticksimport { marked } from "marked";
const html = marked.parse('# \n');
// Creates empty headingimport { marked } from "marked";
const html = marked.parse('[](url)');
// Creates link with empty textimport { marked } from "marked";
const html = marked.parse(' \n \n ');
// Returns space tokenimport { marked } from "marked";
const tokens = marked.lexer('[ref]: url');
console.log(tokens); // []
console.log(tokens.links); // { ref: { href: 'url', title: '' } }import { marked } from "marked";
// Multiple extensions handling same token type
marked.use({
renderer: {
link(token) {
if (token.href.startsWith('mailto:')) {
return `<a href="${token.href}" class="email">...</a>`;
}
return false; // Pass to next renderer
}
}
});
marked.use({
renderer: {
link(token) {
if (token.href.startsWith('tel:')) {
return `<a href="${token.href}" class="phone">...</a>`;
}
return false; // Pass to default
}
}
});import { marked } from "marked";
marked.use({
async: true,
async walkTokens(token) {
if (token.type === 'link') {
try {
await validateUrl(token.href);
} catch (err) {
console.error('Validation error:', err);
token.validated = false;
}
}
}
});
try {
const html = await marked.parse(markdown, { async: true });
} catch (err) {
console.error('Parse error:', err);
}
async function validateUrl(url) {
// Validation logic
}import { marked } from "marked";
const longLine = 'a'.repeat(100000);
const html = marked.parse(longLine);
// Handles long lines efficientlyimport { marked } from "marked";
const manyTokens = '**a** '.repeat(10000);
const html = marked.parse(manyTokens);
// Handles many tokens efficientlyimport { marked } from "marked";
// Convert tokens to JSON
const tokens = marked.lexer(markdown);
const json = JSON.stringify(tokens, null, 2);
// Parse back from JSON
const parsedTokens = JSON.parse(json);
parsedTokens.links = tokens.links; // Restore links
// Parse tokens
const html = marked.parser(parsedTokens);