|
7 | 7 | import { assertEquals, assertStringIncludes } from "testing/asserts"; |
8 | 8 | import { Metadata } from "../../src/config/types.ts"; |
9 | 9 | import { bibliographyCslJson } from "../../src/core/bibliography.ts"; |
10 | | -import { parseHtml } from "../../src/core/deno-dom.ts"; |
| 10 | +import { Element, parseHtml } from "../../src/core/deno-dom.ts"; |
11 | 11 | import { join } from "../../src/deno_ral/path.ts"; |
12 | 12 | import { metadataPostProcessor } from "../../src/format/html/format-html-meta.ts"; |
13 | 13 | import { unitTest } from "../test.ts"; |
@@ -196,3 +196,65 @@ unitTest( |
196 | 196 | }); |
197 | 197 | }, |
198 | 198 | ); |
| 199 | + |
| 200 | +function metaContent( |
| 201 | + doc: Awaited<ReturnType<typeof scholarMetadata>>, |
| 202 | + name: string, |
| 203 | +) { |
| 204 | + return doc.querySelector(`meta[name="${name}"]`)?.getAttribute("content"); |
| 205 | +} |
| 206 | + |
| 207 | +unitTest("Scholar metadata - split the citation page range", async () => { |
| 208 | + for ( |
| 209 | + const [page, firstPage, lastPage] of [ |
| 210 | + ["97-111", "97", "111"], |
| 211 | + ["97–111", "97", "111"], |
| 212 | + ["97 - 111", "97", "111"], |
| 213 | + ["1-3, 7-9", "1", undefined], |
| 214 | + ["42", "42", undefined], |
| 215 | + ] |
| 216 | + ) { |
| 217 | + const doc = await scholarMetadata({ |
| 218 | + title: "An article", |
| 219 | + citation: { page }, |
| 220 | + }); |
| 221 | + assertEquals(metaContent(doc, "citation_firstpage"), firstPage, page); |
| 222 | + assertEquals(metaContent(doc, "citation_lastpage"), lastPage, page); |
| 223 | + } |
| 224 | +}); |
| 225 | + |
| 226 | +unitTest("Scholar metadata - split reference page ranges", async () => { |
| 227 | + await withTempDir(async (dir) => { |
| 228 | + Deno.writeTextFileSync( |
| 229 | + join(dir, "references.yaml"), |
| 230 | + `--- |
| 231 | +references: |
| 232 | +- {id: endash, type: article-journal, title: En dash, page: "97–111"} |
| 233 | +- {id: single, type: article-journal, title: Single, page: "42"} |
| 234 | +- {id: list, type: article-journal, title: List, page: "1-3, 7-9"} |
| 235 | +--- |
| 236 | +`, |
| 237 | + ); |
| 238 | + const doc = await scholarMetadata( |
| 239 | + { title: "An article", bibliography: "references.yaml" }, |
| 240 | + join(dir, "test.qmd"), |
| 241 | + '<span class="citation" data-cites="endash single list"></span>', |
| 242 | + ); |
| 243 | + const references = Object.fromEntries( |
| 244 | + [...doc.querySelectorAll('meta[name="citation_reference"]')].map( |
| 245 | + (element) => { |
| 246 | + const content = (element as Element).getAttribute("content")!; |
| 247 | + return [content.match(/citation_title=([^;]+);/)![1], content]; |
| 248 | + }, |
| 249 | + ), |
| 250 | + ); |
| 251 | + assertStringIncludes( |
| 252 | + references["En dash"], |
| 253 | + "citation_firstpage=97; citation_lastpage=111;", |
| 254 | + ); |
| 255 | + assertStringIncludes(references["Single"], "citation_firstpage=42;"); |
| 256 | + assertEquals(references["Single"].includes("citation_lastpage"), false); |
| 257 | + assertStringIncludes(references["List"], "citation_firstpage=1;"); |
| 258 | + assertEquals(references["List"].includes("citation_lastpage"), false); |
| 259 | + }); |
| 260 | +}); |
0 commit comments