Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/changelog-1.11.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ All changes included in 1.11:
- ([#15051](https://github.com/quarto-dev/quarto-cli/pull/15051)): Fix a callout's `title` option also rendering as an HTML `title` attribute in html, revealjs and epub output, which showed the title as a tooltip over the whole callout.
- ([#15108](https://github.com/quarto-dev/quarto-cli/pull/15108)): Fix Google Scholar metadata showing formatting markup such as `<i>` in `citation_reference` tags for bibliography entries, and `undefined` as the book title of a chapter without one.
- ([#15115](https://github.com/quarto-dev/quarto-cli/pull/15115)): Fix Google Scholar writing two `citation_reference` tags for a reference defined in two bibliography files. The tag now describes the entry Pandoc renders.
- ([#15118](https://github.com/quarto-dev/quarto-cli/pull/15118)): Fix Google Scholar metadata missing the first and last page of bibliography entries, and giving wrong pages for a citation `page` written with an en-dash or as a page list.

### `pdf`

Expand Down
10 changes: 10 additions & 0 deletions src/core/csl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ export function cslTextToPlain(value: string): string {
return value.replace(cslRichTextTags, "");
}

// Splits a CSL `page` value ("97-111", "97–111", "42") into its first and
// last page. A page list ("1-3, 7-9") yields only the first page.
export function cslPageRange(page: string) {
const [range, ...others] = page.split(/[,&]/);
const match = range.match(/^\s*(.*?)\s*[-–—]+\s*(.*?)\s*$/);
const first = (match ? match[1] : range.trim()) || undefined;
const last = (match && others.length === 0 ? match[2] : "") || undefined;
return { first, last };
}

export function suggestId(author: CSLName[], date?: CSLDate) {
// Try to get the last name
let citeIdLeading = "";
Expand Down
13 changes: 9 additions & 4 deletions src/format/html/format-html-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
CSL,
cslDateToEDTFDate,
CSLExtras,
cslPageRange,
cslTextToPlain,
kAbstractUrl,
kEIssn,
Expand Down Expand Up @@ -205,12 +206,16 @@ function googleScholarMeta(
write("citation_language", csl.language);
}

if (csl["page-first"]) {
write("citation_firstpage", csl["page-first"]);
// Bibliography entries from Pandoc carry only `page`.
const pageRange = cslPageRange(`${csl.page ?? ""}`);
const firstPage = csl["page-first"] || pageRange.first;
if (firstPage) {
write("citation_firstpage", firstPage);
}

if (csl["page-last"]) {
write("citation_lastpage", csl["page-last"]);
const lastPage = csl["page-last"] || pageRange.last;
if (lastPage) {
write("citation_lastpage", lastPage);
}

const type = csl.type;
Expand Down
26 changes: 9 additions & 17 deletions src/quarto-core/attribution/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
cslDate,
CSLExtras,
cslNames,
cslPageRange,
CSLType,
cslType,
kAbstractUrl,
Expand Down Expand Up @@ -485,23 +486,14 @@ function pages(citationMetadata: Metadata): PageRange {
let pages = citationMetadata[kPage]
? `${citationMetadata[kPage] as string}` // Force pages to string in case user writes `page: 7`
: undefined;
if (pages && pages.includes("-")) {
const pagesSplit = pages.split("-");
if (!firstPage) {
firstPage = pagesSplit[0];
}

if (!lastPage) {
lastPage = pagesSplit[1];
}
} else if (pages && !firstPage) {
firstPage = pages;
} else if (!pages) {
if (firstPage && lastPage) {
pages = `${firstPage} - ${lastPage}`;
} else if (firstPage) {
pages = `${firstPage}`;
}
if (pages) {
const range = cslPageRange(pages);
firstPage = firstPage || range.first;
lastPage = lastPage || range.last;
} else if (firstPage && lastPage) {
pages = `${firstPage} - ${lastPage}`;
} else if (firstPage) {
pages = `${firstPage}`;
}
return {
firstPage: firstPage as string,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@article{ranged,
author = {Doe, Jane},
title = {Ranged Article},
journal = {Journal of Tests},
year = {2020},
pages = {97--111}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: Scholar reference metadata splits a page range
format: html
google-scholar: true
bibliography: references.bib
_quarto:
tests:
html:
ensureHtmlElements:
- ['meta[name="citation_reference"][content*="citation_firstpage=97;"]', 'meta[name="citation_reference"][content*="citation_lastpage=111;"]']
- []
---

This article cites a work with a page range [@ranged].
64 changes: 63 additions & 1 deletion tests/unit/scholar-metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { assertEquals, assertStringIncludes } from "testing/asserts";
import { Metadata } from "../../src/config/types.ts";
import { bibliographyCslJson } from "../../src/core/bibliography.ts";
import { parseHtml } from "../../src/core/deno-dom.ts";
import { Element, parseHtml } from "../../src/core/deno-dom.ts";
import { join } from "../../src/deno_ral/path.ts";
import { metadataPostProcessor } from "../../src/format/html/format-html-meta.ts";
import { unitTest } from "../test.ts";
Expand Down Expand Up @@ -196,3 +196,65 @@ unitTest(
});
},
);

function metaContent(
doc: Awaited<ReturnType<typeof scholarMetadata>>,
name: string,
) {
return doc.querySelector(`meta[name="${name}"]`)?.getAttribute("content");
}

unitTest("Scholar metadata - split the citation page range", async () => {
for (
const [page, firstPage, lastPage] of [
["97-111", "97", "111"],
["97–111", "97", "111"],
["97 - 111", "97", "111"],
["1-3, 7-9", "1", undefined],
["42", "42", undefined],
]
) {
const doc = await scholarMetadata({
title: "An article",
citation: { page },
});
assertEquals(metaContent(doc, "citation_firstpage"), firstPage, page);
assertEquals(metaContent(doc, "citation_lastpage"), lastPage, page);
}
});

unitTest("Scholar metadata - split reference page ranges", async () => {
await withTempDir(async (dir) => {
Deno.writeTextFileSync(
join(dir, "references.yaml"),
`---
references:
- {id: endash, type: article-journal, title: En dash, page: "97–111"}
- {id: single, type: article-journal, title: Single, page: "42"}
- {id: list, type: article-journal, title: List, page: "1-3, 7-9"}
---
`,
);
const doc = await scholarMetadata(
{ title: "An article", bibliography: "references.yaml" },
join(dir, "test.qmd"),
'<span class="citation" data-cites="endash single list"></span>',
);
const references = Object.fromEntries(
[...doc.querySelectorAll('meta[name="citation_reference"]')].map(
(element) => {
const content = (element as Element).getAttribute("content")!;
return [content.match(/citation_title=([^;]+);/)![1], content];
},
),
);
assertStringIncludes(
references["En dash"],
"citation_firstpage=97; citation_lastpage=111;",
);
assertStringIncludes(references["Single"], "citation_firstpage=42;");
assertEquals(references["Single"].includes("citation_lastpage"), false);
assertStringIncludes(references["List"], "citation_firstpage=1;");
assertEquals(references["List"].includes("citation_lastpage"), false);
});
});
Loading