Files
plainleaf/client/markdown_renderer/markdown_render.test.ts
T
Zef Hemel dbc95239f6 Fixes #702 implementing Live Preview for HTML tags
This may still have edge cases, but basics should be working.
2026-03-31 11:11:17 +02:00

192 lines
4.5 KiB
TypeScript

import { expect, test } from "vitest";
import { parse } from "../markdown_parser/parse_tree.ts";
import { renderMarkdownToHtml } from "./markdown_render.ts";
import { extendedMarkdownLanguage } from "../markdown_parser/parser.ts";
const sampleMarkdown = `---
name: Sup
---
# Hello world
This is **bold** and _italic_, or _italic_. And a **_mix_**. And ==highlight==!
Lists:
- This
- Is a
- list
- And here we go nested
1. This is a numbered
2. Two
- And different
- Bla
- More bla
And:
1. Numbered
2. Two
## Second heading
And some
And like this:
More code
Bla
And a blockquote:
> Sup yo Empty line\
> Second part
<!-- this is a comment -->
And more custom stuff [[Page link]]
{[Command button]}
- [ ] #next Task
- [x] #next Task 2
## Tables
| type | actor_login | created_at | payload_ref |
| --------- | ----------- | -------------------- | ---------------------- |
| PushEvent | avb | 2022-10-27T08:27:48Z | refs/heads/master |
| PushEvent | avb | 2022-10-27T04:31:27Z | refs/heads/jitterSched |
Here is something
---
A new thing.
`;
test("Markdown render", () => {
const tree = parse(extendedMarkdownLanguage, sampleMarkdown);
renderMarkdownToHtml(tree, {
failOnUnknown: true,
});
});
test("Wiki link with embedded image path", () => {
// This particular one caused an infinite regex loop previously, adding it here as a regression to avoid in the future
const example = `![[Inbox/2026-01-08/CleanShot 2026-01-01 at 12.36.23.png]]`;
const tree = parse(extendedMarkdownLanguage, example);
renderMarkdownToHtml(tree, {
failOnUnknown: true,
});
});
test("Smart hard break test", () => {
const example = `**Hello**
*world!*`;
const tree = parse(extendedMarkdownLanguage, example);
const html = renderMarkdownToHtml(tree, {
failOnUnknown: true,
smartHardBreak: true,
});
expect(html).toEqual(
`<span class="p"><strong>Hello</strong><br><em>world!</em></span>`,
);
const example2 = `This is going to be a text. With a new line.
And another
* and a list
* with a second item
### [[Bla]]
Url: something
Server: something else
📅 last_updated - [Release notes](release_notes_url)`;
const tree2 = parse(extendedMarkdownLanguage, example2);
const html2 = renderMarkdownToHtml(tree2, {
failOnUnknown: true,
smartHardBreak: true,
});
console.log(html2);
});
test("Inline HTML tags render as proper elements", () => {
const tree = parse(extendedMarkdownLanguage, "<marquee>Hello</marquee>");
const html = renderMarkdownToHtml(tree, { failOnUnknown: true });
expect(html).toEqual(
'<span class="p"><marquee>Hello</marquee></span>',
);
});
test("Inline HTML tags with attributes", () => {
const tree = parse(
extendedMarkdownLanguage,
'<span style="color:red">red text</span>',
);
const html = renderMarkdownToHtml(tree, { failOnUnknown: true });
expect(html).toEqual(
'<span class="p"><span style="color:red">red text</span></span>',
);
});
test("Inline HTML tags with markdown content", () => {
const tree = parse(
extendedMarkdownLanguage,
'<marquee class="x">Hello **there**</marquee>',
);
const html = renderMarkdownToHtml(tree, { failOnUnknown: true });
expect(html).toEqual(
'<span class="p"><marquee class="x">Hello <strong>there</strong></marquee></span>',
);
});
test("Inline HTML mixed with text", () => {
const tree = parse(
extendedMarkdownLanguage,
"Before <b>bold</b> after",
);
const html = renderMarkdownToHtml(tree, { failOnUnknown: true });
expect(html).toEqual(
'<span class="p">Before <b>bold</b> after</span>',
);
});
test("Nested same-name HTML tags", () => {
const tree = parse(
extendedMarkdownLanguage,
"<b><b>nested</b></b>",
);
const html = renderMarkdownToHtml(tree, { failOnUnknown: true });
expect(html).toEqual(
'<span class="p"><b><b>nested</b></b></span>',
);
});
test("Inline HTML with wiki link", () => {
const tree = parse(
extendedMarkdownLanguage,
"<span>hello [[there]]</span>",
);
const html = renderMarkdownToHtml(tree, { failOnUnknown: true });
expect(html).toEqual(
'<span class="p"><span>hello <a href="/there" class="wiki-link" data-ref="there">there</a></span></span>',
);
});
test("Unmatched HTML tags render as literal text", () => {
const tree = parse(
extendedMarkdownLanguage,
"text <b>unclosed",
);
const html = renderMarkdownToHtml(tree, { failOnUnknown: true });
expect(html).toEqual(
'<span class="p">text &lt;b&gt;unclosed</span>',
);
});