hi"), {
text: "hi",
entities: [{ type: "bold", offset: 0, length: 2 }],
});
assert.deepEqual(
htmlToEntities("ab").entities.sort((x, y) => x.offset - y.offset),
[
{ type: "bold", offset: 0, length: 2 },
{ type: "italic", offset: 1, length: 1 },
],
);
});
test("html: unmatched closing tag is dropped, unknown tags stay literal", () => {
assert.deepEqual(htmlToEntities("ac"), { text: "ac", entities: [] });
assert.deepEqual(htmlToEntities("x
"), { text: "x", entities: [] });
});
test("md: full dialect — bold, italics, underline, strike, spoiler", () => {
assert.deepEqual(md`**a** *b* _c_ __d__ ~~e~~ ||f||`, {
text: "a b c d e f",
entities: [
{ type: "bold", offset: 0, length: 1 },
{ type: "italic", offset: 2, length: 1 },
{ type: "italic", offset: 4, length: 1 },
{ type: "underline", offset: 6, length: 1 },
{ type: "strikethrough", offset: 8, length: 1 },
{ type: "spoiler", offset: 10, length: 1 },
],
});
});
test("md: single delimiters don't trigger mid-word or around whitespace", () => {
assert.deepEqual(mdToEntities("snake_case_name"), { text: "snake_case_name", entities: [] });
assert.deepEqual(mdToEntities("a * b * c"), { text: "a * b * c", entities: [] });
assert.deepEqual(mdToEntities("2 * 3 = 6"), { text: "2 * 3 = 6", entities: [] });
});
test("md: > lines form one blockquote entity", () => {
assert.deepEqual(mdToEntities("> quoted\n> more\nafter"), {
text: "quoted\nmore\nafter",
entities: [{ type: "blockquote", offset: 0, length: 11 }],
});
// inline markup keeps working inside the quote; mid-line > stays literal
const r = mdToEntities("> **b**");
assert.equal(r.text, "b");
assert.deepEqual(
r.entities.sort((x, y) => y.length - x.length),
[
{ type: "blockquote", offset: 0, length: 1 },
{ type: "bold", offset: 0, length: 1 },
],
);
assert.deepEqual(mdToEntities("a > b"), { text: "a > b", entities: [] });
});
test("md: inline code and fenced pre with language", () => {
assert.deepEqual(mdToEntities("run `npm i`"), {
text: "run npm i",
entities: [{ type: "code", offset: 4, length: 5 }],
});
// the newline before the closing fence is not part of the body
assert.deepEqual(mdToEntities("```js\nconst x = 1\n```"), {
text: "const x = 1",
entities: [{ type: "pre", offset: 0, length: 11, language: "js" }],
});
});
test("md: link", () => {
assert.deepEqual(md`see [docs](https://yaebal.mom)`, {
text: "see docs",
entities: [{ type: "text_link", offset: 4, length: 4, url: "https://yaebal.mom" }],
});
});
test("md: interpolated string is literal (delimiters not parsed)", () => {
const r = md`**${"**not bold**"}**`;
assert.equal(r.text, "**not bold**");
assert.deepEqual(r.entities, [{ type: "bold", offset: 0, length: 12 }]);
});
test("md: interpolation inside a link url is substituted", () => {
const r = md`[docs](${"https://yaebal.mom"})`;
assert.deepEqual(r, {
text: "docs",
entities: [{ type: "text_link", offset: 0, length: 4, url: "https://yaebal.mom" }],
});
});
test("md: backslash escapes a delimiter", () => {
assert.deepEqual(mdToEntities("2 \\* 3 = 6"), { text: "2 * 3 = 6", entities: [] });
assert.deepEqual(mdToEntities("\\*\\*not bold\\*\\*"), { text: "**not bold**", entities: [] });
// an escaped delimiter inside a run stays literal and doesn't end the run
assert.deepEqual(mdToEntities("**a \\** b**"), {
text: "a ** b",
entities: [{ type: "bold", offset: 0, length: 6 }],
});
});
test("md: nested formatting keeps correct offsets", () => {
const r = md`**bold ${"and"} [more](https://yaebal.mom)**`;
assert.equal(r.text, "bold and more");
const boldE = r.entities.find((e) => e.type === "bold");
const link = r.entities.find((e) => e.type === "text_link");
assert.deepEqual(boldE, { type: "bold", offset: 0, length: 13 });
assert.deepEqual(link, { type: "text_link", offset: 9, length: 4, url: "https://yaebal.mom" });
});
test("md: a FormatResult sub merges, entity spanning the slot stretches", () => {
const r = md`**a${italic("BBB")}c**`;
assert.equal(r.text, "aBBBc");
assert.deepEqual(
r.entities.sort((x, y) => y.length - x.length),
[
{ type: "bold", offset: 0, length: 5 },
{ type: "italic", offset: 1, length: 3 },
],
);
});