Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
import { describe, expect, test, vi } from "vitest";
import {
createMarkdownFrontmatterDiagnostics,
extractMarkdownBody,
extractMarkdownBodyAndExcerpt,
extractMarkdownExcerpt,
extractMarkdownFrontmatter,
MarkdownMetadataError,
} from "./markdown";
import { contentEngineLimits } from "./limits";
describe("extractMarkdownFrontmatter", () => {
test("parses schema-less nested YAML without retaining the body", async () => {
const result = await extractMarkdownFrontmatter(`---
title: Hello
publishedAt: 2026-07-18
draft: false
author:
name: Oleg
tags: [web, studio]
---
# A body that is not metadata
`);
expect(result.properties).toEqual({
title: "Hello",
publishedAt: "2026-07-18",
draft: false,
author: { name: "Oleg" },
tags: ["web", "studio"],
});
expect(result.frontmatterBytes).toBeGreaterThan(0);
expect(result.consumedBytes).toBeLessThan(
new TextEncoder().encode(`---
title: Hello
publishedAt: 2026-07-18
draft: false
author:
name: Oleg
tags: [web, studio]
---
# A body that is not metadata
`).byteLength
);
});
test("stops consuming a stream after the closing delimiter", async () => {
const returned = vi.fn();
const source = {
async *[Symbol.asyncIterator]() {
try {
yield new TextEncoder().encode("---\ntitle: Hello\n---\n");
yield new TextEncoder().encode("body must not be read");
} finally {
returned();
}
},
};
await expect(extractMarkdownFrontmatter(source)).resolves.toMatchObject({
properties: { title: "Hello" },
});
expect(returned).toHaveBeenCalledOnce();
});
test("returns empty properties after reading only the opening line", async () => {
const result = await extractMarkdownFrontmatter("# No frontmatter\nbody");
expect(result.properties).toEqual({});
expect(result.consumedBytes).toBeLessThan(20);
});
test("parses an explicit empty frontmatter block", async () => {
await expect(
extractMarkdownFrontmatter("---\n---\nBody")
).resolves.toMatchObject({ properties: {} });
});
test("supports a UTF-8 byte order mark and CRLF delimiters", async () => {
await expect(
extractMarkdownFrontmatter("\ufeff---\r\ntitle: Hello\r\n---\r\nBody")
).resolves.toMatchObject({ properties: { title: "Hello" } });
});
test("rejects invalid YAML with a stable error code", async () => {
await expect(
extractMarkdownFrontmatter("---\ntitle: [broken\n---\n")
).rejects.toMatchObject({
code: "FRONTMATTER_INVALID",
line: 3,
column: 1,
} satisfies Partial<MarkdownMetadataError>);
});
test("reports every YAML parser error", async () => {
await expect(
createMarkdownFrontmatterDiagnostics("---\na: 1\na: 2\nb: 1\nb: 2\n---\n")
).resolves.toMatchObject([
{ code: "FRONTMATTER_INVALID", line: 3, column: 1 },
{ code: "FRONTMATTER_INVALID", line: 5, column: 1 },
]);
});
test("uses the YAML parser error as the frontmatter title and reason", async () => {
const [diagnostic] = await createMarkdownFrontmatterDiagnostics(
'---\ntitle: "Broken article\npublished: true\n---\n'
);
expect(diagnostic.message.startsWith("Missing closing")).toBe(true);
expect(diagnostic.reason).toBe(diagnostic.message);
});
test("reports every independent post-parse frontmatter limit", async () => {
const fields = Array.from(
{ length: contentEngineLimits.frontmatterFields + 1 },
(_, index) => `field${index}: ${index}`
).join("\n");
const depth = Array.from(
{ length: contentEngineLimits.frontmatterDepth + 1 },
(_, index) => `${" ".repeat(index)}depth${index}:`
).join("\n");
const longString = "x".repeat(
contentEngineLimits.frontmatterStringBytes + 1
);
const compactValues = Array.from({ length: 17_000 }, () => "x").join(",");
await expect(
createMarkdownFrontmatterDiagnostics(
`---\n${depth}\n${" ".repeat(contentEngineLimits.frontmatterDepth + 1)}value\n${fields}\nlong: ${longString}\ncompact: [${compactValues}]\n---\n`
)
).resolves.toMatchObject([
{ code: "FRONTMATTER_DEPTH_EXCEEDED", severity: "warning" },
{ code: "FRONTMATTER_FIELDS_EXCEEDED", severity: "warning" },
{ code: "FRONTMATTER_STRING_BYTES_EXCEEDED", severity: "warning" },
{ code: "FRONTMATTER_BYTES_EXCEEDED", severity: "warning" },
]);
});
test("reports every non-JSON frontmatter value at its source location", async () => {
await expect(
createMarkdownFrontmatterDiagnostics(
"---\nfirst: .nan\nsecond: .inf\nthird: 9007199254740993\n---\n"
)
).resolves.toMatchObject([
{ code: "FRONTMATTER_INVALID", line: 2, column: 8 },
{ code: "FRONTMATTER_INVALID", line: 3, column: 9 },
{ code: "FRONTMATTER_INVALID", line: 4, column: 8 },
]);
});
test("rejects an unclosed block at the byte boundary", async () => {
await expect(
extractMarkdownFrontmatter("---\ntitle: too large", { bytes: 8 })
).rejects.toMatchObject({
code: "FRONTMATTER_BYTES_EXCEEDED",
} satisfies Partial<MarkdownMetadataError>);
});
test("rejects a closed block whose properties exceed the byte limit", async () => {
await expect(
extractMarkdownFrontmatter(
`---\ntitle: ${"large".repeat(20)}\n---\nBody`,
{ bytes: 32 }
)
).rejects.toMatchObject({
code: "FRONTMATTER_BYTES_EXCEEDED",
} satisfies Partial<MarkdownMetadataError>);
});
test("enforces nesting, field, and string limits", async () => {
await expect(
extractMarkdownFrontmatter("---\na:\n b:\n c: value\n---\n", {
depth: 2,
})
).rejects.toMatchObject({ code: "FRONTMATTER_DEPTH_EXCEEDED" });
await expect(
extractMarkdownFrontmatter("---\na: 1\nb: 2\n---\n", { fields: 1 })
).rejects.toMatchObject({ code: "FRONTMATTER_FIELDS_EXCEEDED" });
await expect(
extractMarkdownFrontmatter("---\ntitle: long\n---\n", { stringBytes: 3 })
).rejects.toMatchObject({ code: "FRONTMATTER_STRING_BYTES_EXCEEDED" });
await expect(
extractMarkdownFrontmatter("---\ntitle: long\n---\n", {
serializedBytes: 8,
})
).rejects.toMatchObject({ code: "FRONTMATTER_BYTES_EXCEEDED" });
});
test("rejects aliases and duplicate keys", async () => {
await expect(
extractMarkdownFrontmatter("---\nbase: &base value\ncopy: *base\n---\n")
).rejects.toMatchObject({ code: "FRONTMATTER_INVALID" });
await expect(
extractMarkdownFrontmatter("---\ntitle: first\ntitle: second\n---\n")
).rejects.toMatchObject({ code: "FRONTMATTER_INVALID" });
});
test("rejects tagged YAML values that are not JSON", async () => {
for (const value of [
"!!timestamp 2020-01-01",
"!!set\n item: null",
"!!omap\n - item: value",
]) {
await expect(
extractMarkdownFrontmatter(`---\nvalue: ${value}\n---\n`)
).rejects.toMatchObject({ code: "FRONTMATTER_INVALID" });
}
});
test("preserves arbitrary object keys without prototype pollution", async () => {
const { properties } = await extractMarkdownFrontmatter(`---
__proto__:
polluted: true
constructor: user value
prototype: another value
---
`);
expect(Object.hasOwn(properties, "__proto__")).toBe(true);
expect(properties["__proto__"]).toEqual({ polluted: true });
expect(properties.constructor).toBe("user value");
expect(properties.prototype).toBe("another value");
expect(({} as { polluted?: boolean }).polluted).toBeUndefined();
});
test("rejects integers that cannot be represented without rounding", async () => {
await expect(
extractMarkdownFrontmatter("---\nvalue: 9007199254740993\n---\n")
).rejects.toMatchObject({ code: "FRONTMATTER_INVALID" });
});
});
describe("Markdown body and excerpt extraction", () => {
test("removes frontmatter while preserving complete Markdown body", async () => {
const source = "---\ntitle: Hello\n---\n# Heading\n\nBody **text**.";
const body = "# Heading\n\nBody **text**.";
await expect(extractMarkdownBody(source)).resolves.toEqual({
body,
bodyBytes: new TextEncoder().encode(body).byteLength,
sourceBytes: new TextEncoder().encode(source).byteLength,
});
});
test("does not treat a later thematic break as frontmatter", async () => {
await expect(
extractMarkdownBody("Introduction\n\n---\n\nMore")
).resolves.toMatchObject({ body: "Introduction\n\n---\n\nMore" });
});
test("removes a UTF-8 byte order mark without frontmatter", async () => {
await expect(extractMarkdownBody("\ufeff# Heading")).resolves.toMatchObject(
{
body: "# Heading",
}
);
});
test("enforces the complete-file byte limit before decoding", async () => {
await expect(extractMarkdownBody("123456", 5)).rejects.toMatchObject({
code: "MARKDOWN_BODY_BYTES_EXCEEDED",
});
});
test("rejects invalid UTF-8", async () => {
await expect(
extractMarkdownBody(new Uint8Array([0xc3, 0x28]))
).rejects.toMatchObject({ code: "MARKDOWN_BODY_DECODING_FAILED" });
});
test("creates plain-text excerpts without markup or raw HTML", () => {
expect(
extractMarkdownExcerpt(
"# Hello *world*\n\n<div>hidden</div>\n\nUse `code` and [links](https://example.com)."
)
).toBe("Hello world Use code and links.");
});
test("uses GFM semantics in plain-text excerpts", () => {
expect(extractMarkdownExcerpt("- [x] Ready\n\n~~obsolete~~")).toBe(
"Ready obsolete"
);
});
test("truncates excerpts at a valid UTF-8 character boundary", () => {
expect(extractMarkdownExcerpt("Hello 🌍 world", 10)).toBe("Hello 🌍");
expect(
new TextEncoder().encode(extractMarkdownExcerpt("🌍🌍", 5))
).toHaveLength(4);
});
test("extracts a bounded body and excerpt in one operation", async () => {
await expect(
extractMarkdownBodyAndExcerpt(
"---\ntitle: Hello\n---\n# Heading\n\nLong body text",
{ excerptBytes: 12 }
)
).resolves.toMatchObject({
body: "# Heading\n\nLong body text",
excerpt: "Heading Long",
});
});
test("does not allow callers to raise shared limits", async () => {
await expect(
extractMarkdownBody("Body", 1024 * 1024 + 1)
).rejects.toMatchObject({ code: "MARKDOWN_BODY_BYTES_EXCEEDED" });
expect(() => extractMarkdownExcerpt("Body", 2049)).toThrowError(
expect.objectContaining({ code: "MARKDOWN_EXCERPT_BYTES_EXCEEDED" })
);
});
});