feat: markdown links render in preview
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import nunjucks from "nunjucks";
|
||||
import path from "node:path";
|
||||
import { filters } from "../src/_filters/index.js";
|
||||
|
||||
const includesDir = path.resolve("src/_includes");
|
||||
const markdownExcerpt = "Read the [deployment guide](/guides/deploy/) before continuing.";
|
||||
|
||||
function renderTemplate(template, context) {
|
||||
const env = new nunjucks.Environment(new nunjucks.FileSystemLoader(includesDir), { autoescape: false });
|
||||
for (const [name, filter] of Object.entries(filters)) {
|
||||
env.addFilter(name, filter);
|
||||
}
|
||||
|
||||
return env.render(template, context);
|
||||
}
|
||||
|
||||
test("Excerpt Markdown: renders Markdown links as safe inline HTML", () => {
|
||||
assert.equal(
|
||||
filters.renderExcerptMarkdown(markdownExcerpt),
|
||||
'Read the <a href="/guides/deploy/">deployment guide</a> before continuing.'
|
||||
);
|
||||
});
|
||||
|
||||
test("Excerpt Markdown: renders links in post-card previews", () => {
|
||||
const output = renderTemplate("partials/post-card.njk", {
|
||||
post: {
|
||||
fileSlug: "example-dispatch",
|
||||
url: "/dispatches/example/",
|
||||
date: "2026-10-06",
|
||||
data: {
|
||||
title: "Example dispatch",
|
||||
excerpt: markdownExcerpt,
|
||||
tags: ["systems"]
|
||||
},
|
||||
templateContent: "A short article."
|
||||
}
|
||||
});
|
||||
|
||||
assert.match(output, /<p class="feed-excerpt">\s*Read the <a href="\/guides\/deploy\/">deployment guide<\/a> before continuing\.\s*<\/p>/);
|
||||
});
|
||||
|
||||
test("Excerpt Markdown: renders links in featured previews and escapes raw HTML", () => {
|
||||
const output = renderTemplate("partials/featured-card.njk", {
|
||||
collections: {
|
||||
posts: [{
|
||||
url: "/dispatches/example/",
|
||||
date: "2026-10-06",
|
||||
data: {
|
||||
featured: true,
|
||||
tags: ["systems"],
|
||||
title: "Example dispatch",
|
||||
excerpt: `${markdownExcerpt} <script>alert('unsafe')</script>`
|
||||
},
|
||||
templateContent: ""
|
||||
}]
|
||||
}
|
||||
});
|
||||
|
||||
assert.match(output, /<a href="\/guides\/deploy\/">deployment guide<\/a>/);
|
||||
assert.match(output, /<script>alert\('unsafe'\)<\/script>/);
|
||||
assert.doesNotMatch(output, /<script>/);
|
||||
});
|
||||
Reference in New Issue
Block a user