File size: 11,328 Bytes
3de227d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import { describe, it, expect } from "vitest";
import {
  renderArticleMarkdown,
  stripHtmlToText,
} from "../src/publisher/markdown-renderer.js";
import type { PublishMeta, CitationData } from "../src/publisher/html-renderer.js";

const META: PublishMeta = {
  title: "Test Article",
  subtitle: "A subtitle",
  description: "A short description for SEO",
  authors: [
    { name: "Alice", affiliationIndices: [1], affiliationNames: ["MIT"] },
    { name: "Bob", affiliationIndices: [2], affiliationNames: ["HF"] },
  ],
  affiliations: [{ name: "MIT" }, { name: "HF" }],
  date: "2026-04-30",
  doi: "10.1234/abcd.efgh",
};

const doc = (content: any[]) => ({ type: "doc", content });

describe("renderArticleMarkdown - header", () => {
  it("emits an llms.txt-style header with title, description, authors, date and DOI", () => {
    const md = renderArticleMarkdown(doc([{ type: "paragraph" }]), META);
    expect(md).toContain("# Test Article");
    expect(md).toContain("> A short description for SEO");
    expect(md).toContain("- **Authors**: Alice, Bob");
    expect(md).toContain("- **Published**: 2026-04-30");
    expect(md).toContain("- **DOI**: https://doi.org/10.1234/abcd.efgh");
    expect(md).toContain("---");
  });

  it("falls back to subtitle when description is empty", () => {
    const md = renderArticleMarkdown(
      doc([{ type: "paragraph" }]),
      { ...META, description: "" },
    );
    expect(md).toContain("> A subtitle");
  });

  it("collapses multi-line titles", () => {
    const md = renderArticleMarkdown(
      doc([{ type: "paragraph" }]),
      { ...META, title: "Line one\\nLine two" },
    );
    expect(md).toContain("# Line one Line two");
    expect(md).not.toContain("\\n");
  });
});

describe("renderArticleMarkdown - block nodes", () => {
  it("renders headings with the correct markdown level", () => {
    const md = renderArticleMarkdown(
      doc([
        { type: "heading", attrs: { level: 2 }, content: [{ type: "text", text: "Hello" }] },
        { type: "heading", attrs: { level: 3 }, content: [{ type: "text", text: "Sub" }] },
      ]),
      META,
    );
    expect(md).toContain("## Hello");
    expect(md).toContain("### Sub");
  });

  it("applies bold/italic/code/link marks", () => {
    const md = renderArticleMarkdown(
      doc([
        {
          type: "paragraph",
          content: [
            { type: "text", text: "bold", marks: [{ type: "bold" }] },
            { type: "text", text: " " },
            { type: "text", text: "italic", marks: [{ type: "italic" }] },
            { type: "text", text: " " },
            { type: "text", text: "code", marks: [{ type: "code" }] },
            { type: "text", text: " " },
            {
              type: "text",
              text: "link",
              marks: [{ type: "link", attrs: { href: "https://example.com" } }],
            },
          ],
        },
      ]),
      META,
    );
    expect(md).toContain("**bold**");
    expect(md).toContain("*italic*");
    expect(md).toContain("`code`");
    expect(md).toContain("[link](https://example.com)");
  });

  it("renders bullet and ordered lists", () => {
    const md = renderArticleMarkdown(
      doc([
        {
          type: "bulletList",
          content: [
            { type: "listItem", content: [{ type: "paragraph", content: [{ type: "text", text: "one" }] }] },
            { type: "listItem", content: [{ type: "paragraph", content: [{ type: "text", text: "two" }] }] },
          ],
        },
        {
          type: "orderedList",
          content: [
            { type: "listItem", content: [{ type: "paragraph", content: [{ type: "text", text: "first" }] }] },
            { type: "listItem", content: [{ type: "paragraph", content: [{ type: "text", text: "second" }] }] },
          ],
        },
      ]),
      META,
    );
    expect(md).toContain("- one");
    expect(md).toContain("- two");
    expect(md).toContain("1. first");
    expect(md).toContain("2. second");
  });

  it("renders code blocks with language fence", () => {
    const md = renderArticleMarkdown(
      doc([
        {
          type: "codeBlock",
          attrs: { language: "ts" },
          content: [{ type: "text", text: "const x = 1;" }],
        },
      ]),
      META,
    );
    expect(md).toContain("```ts");
    expect(md).toContain("const x = 1;");
    expect(md).toContain("```");
  });

  it("renders inline and block math", () => {
    const md = renderArticleMarkdown(
      doc([
        {
          type: "paragraph",
          content: [
            { type: "text", text: "Energy: " },
            { type: "inlineMath", attrs: { latex: "E = mc^2" } },
          ],
        },
        { type: "blockMath", attrs: { latex: "\\int_0^1 x dx" } },
      ]),
      META,
    );
    expect(md).toContain("$E = mc^2$");
    expect(md).toContain("$$\n\\int_0^1 x dx\n$$");
  });

  it("renders tables with a header row separator", () => {
    const md = renderArticleMarkdown(
      doc([
        {
          type: "table",
          content: [
            {
              type: "tableRow",
              content: [
                { type: "tableHeader", content: [{ type: "paragraph", content: [{ type: "text", text: "Col A" }] }] },
                { type: "tableHeader", content: [{ type: "paragraph", content: [{ type: "text", text: "Col B" }] }] },
              ],
            },
            {
              type: "tableRow",
              content: [
                { type: "tableCell", content: [{ type: "paragraph", content: [{ type: "text", text: "1" }] }] },
                { type: "tableCell", content: [{ type: "paragraph", content: [{ type: "text", text: "2" }] }] },
              ],
            },
          ],
        },
      ]),
      META,
    );
    expect(md).toContain("| Col A | Col B |");
    expect(md).toContain("| --- | --- |");
    expect(md).toContain("| 1 | 2 |");
  });
});

describe("renderArticleMarkdown - custom components", () => {
  it("collapses HtmlEmbed to a single inline placeholder with title and src", () => {
    const md = renderArticleMarkdown(
      doc([
        {
          type: "htmlEmbed",
          attrs: { src: "d3-chart.html", title: "Citations over time", desc: "" },
        },
      ]),
      META,
    );
    expect(md).toContain("*[Interactive visualization: Citations over time]*");
    expect(md).not.toContain("<iframe");
  });

  it("renders Note as a blockquote", () => {
    const md = renderArticleMarkdown(
      doc([
        {
          type: "note",
          content: [
            { type: "paragraph", content: [{ type: "text", text: "Heads up." }] },
          ],
        },
      ]),
      META,
    );
    expect(md).toContain("> Heads up.");
  });

  it("renders Accordion with bold title and inner content", () => {
    const md = renderArticleMarkdown(
      doc([
        {
          type: "accordion",
          attrs: { title: "More details" },
          content: [
            { type: "paragraph", content: [{ type: "text", text: "Inside." }] },
          ],
        },
      ]),
      META,
    );
    expect(md).toContain("**More details**");
    expect(md).toContain("Inside.");
  });

  it("renders QuoteBlock with attribution", () => {
    const md = renderArticleMarkdown(
      doc([
        {
          type: "quoteBlock",
          attrs: { author: "Ada Lovelace", source: "Notes" },
          content: [
            { type: "paragraph", content: [{ type: "text", text: "The future is open." }] },
          ],
        },
      ]),
      META,
    );
    expect(md).toContain("> The future is open.");
    expect(md).toContain("> -- Ada Lovelace, Notes");
  });

  it("renders HfUser as a markdown link to huggingface.co/<u>", () => {
    const md = renderArticleMarkdown(
      doc([
        {
          type: "hfUser",
          attrs: { username: "tfrere", name: "Thibaud Frere" },
        },
      ]),
      META,
    );
    expect(md).toContain("[Thibaud Frere](https://huggingface.co/tfrere)");
  });

  it("renders Mermaid as a fenced ```mermaid block", () => {
    const md = renderArticleMarkdown(
      doc([
        {
          type: "mermaid",
          attrs: { code: "graph TD\n  A --> B" },
        },
      ]),
      META,
    );
    expect(md).toContain("```mermaid");
    expect(md).toContain("graph TD");
    expect(md).toContain("A --> B");
  });

  it("unwraps Wide / FullWidth / Stack containers", () => {
    const md = renderArticleMarkdown(
      doc([
        {
          type: "wide",
          content: [
            { type: "paragraph", content: [{ type: "text", text: "Wide content." }] },
          ],
        },
      ]),
      META,
    );
    expect(md).toContain("Wide content.");
    expect(md).not.toContain("[wide]");
  });
});

describe("renderArticleMarkdown - citations and footnotes", () => {
  it("renders citations as keys for APA and as numeric tags for IEEE", () => {
    const json = doc([
      {
        type: "paragraph",
        content: [
          { type: "text", text: "See " },
          { type: "citation", attrs: { key: "smith2024", label: "Smith (2024)" } },
          { type: "text", text: "." },
        ],
      },
    ]);
    const apa: CitationData = {
      entries: [{ id: "smith2024" }],
      orderedKeys: ["smith2024"],
      style: "apa",
    };
    const ieee: CitationData = {
      entries: [{ id: "smith2024" }],
      orderedKeys: ["smith2024"],
      style: "ieee",
    };
    expect(renderArticleMarkdown(json, META, apa)).toContain("Smith (2024)");
    expect(renderArticleMarkdown(json, META, ieee)).toContain("[1]");
  });

  it("collects footnotes and emits a footnotes section", () => {
    const md = renderArticleMarkdown(
      doc([
        {
          type: "paragraph",
          content: [
            { type: "text", text: "Body" },
            { type: "footnote", attrs: { content: "First note" } },
            { type: "text", text: " more " },
            { type: "footnote", attrs: { content: "Second note" } },
          ],
        },
      ]),
      META,
    );
    expect(md).toContain("[^1]");
    expect(md).toContain("[^2]");
    expect(md).toContain("## Footnotes");
    expect(md).toContain("[^1]: First note");
    expect(md).toContain("[^2]: Second note");
  });

  it("appends a References section from the formatted bibliography", () => {
    const biblio = '<div class="csl-entry">Smith, J. (2024). <i>Test Paper</i>. Journal.</div>';
    const md = renderArticleMarkdown(
      doc([{ type: "paragraph", content: [{ type: "text", text: "Body" }] }]),
      META,
      undefined,
      biblio,
    );
    expect(md).toContain("## References");
    expect(md).toContain("Smith, J. (2024).");
    expect(md).toContain("Test Paper");
    expect(md).not.toContain("<div");
  });
});

describe("stripHtmlToText", () => {
  it("converts <a href> to a markdown link", () => {
    expect(stripHtmlToText('<a href="https://example.com">click</a>')).toBe(
      "[click](https://example.com)",
    );
  });

  it("decodes common HTML entities", () => {
    expect(stripHtmlToText("Tom &amp; Jerry &lt;3")).toBe("Tom & Jerry <3");
  });

  it("collapses block tags into newlines and removes the rest", () => {
    const html = "<p>One.</p><p>Two.</p>";
    expect(stripHtmlToText(html).trim()).toBe("One.\nTwo.");
  });
});