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
5 changes: 5 additions & 0 deletions .changeset/silly-phones-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'citation-js-utils': patch
---

Decode HTML entities in CrossRef citation data
2 changes: 2 additions & 0 deletions packages/citation-js-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
"@citation-js/core": "^0.7.18",
"@citation-js/plugin-bibtex": "^0.7.18",
"@citation-js/plugin-csl": "^0.7.18",
"he": "^1.2.0",
"doi-utils": "^2.0.5",
"sanitize-html": "^2.7.0"
},
"devDependencies": {
"@types/he": "^1.2.0",
"@types/sanitize-html": "^2.6.2"
}
}
6 changes: 4 additions & 2 deletions packages/citation-js-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Cite, plugins } from '@citation-js/core';
import { doi as doiUtils } from 'doi-utils';
import { clean as cleanCSL } from '@citation-js/core/lib/plugins/input/csl.js';
import sanitizeHtml from 'sanitize-html';
import he from 'he';

import '@citation-js/plugin-bibtex';
import '@citation-js/plugin-csl';
Expand Down Expand Up @@ -267,7 +268,8 @@ export function getCitationRenderers(data: CSL[]): CitationRenderer {
if (!c.id) {
c.id = formatLabel(c);
}
// Trim the titles, DOIs, etc. on load
// Trim the titles, DOIs, etc. on load; decode HTML entities from
// CrossRef metadata.
[
'title',
'note',
Expand All @@ -279,7 +281,7 @@ export function getCitationRenderers(data: CSL[]): CitationRenderer {
'DOI',
'ISSN',
].forEach((tag) => {
if (c[tag] && typeof c[tag] === 'string') c[tag] = c[tag].trim();
if (c[tag] && typeof c[tag] === 'string') c[tag] = he.decode(c[tag].trim());
});
// Trim the DOIs and URLs (these are encoded) on load
if (c.URL) c.URL = c.URL.replace(/^(%20)*/, '').replace(/(%20)*$/, '');
Expand Down
20 changes: 20 additions & 0 deletions packages/citation-js-utils/tests/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ describe('yearFromCitation', () => {
});
});

describe('HTML entity decoding in CSL fields', () => {
it('decodes HTML entities in title and container-title from CrossRef CSL-JSON', () => {
const csl = [
{
id: 'test2024',
type: 'article-journal',
title: 'Signal & Image Processing & More',
'container-title': 'Computers & Geosciences',
author: [{ family: 'Smith', given: 'J.' }],
issued: { 'date-parts': [[2024]] },
DOI: '10.1234/test',
},
];
const renderers = getCitationRenderers(csl);
const cite = renderers['test2024'];
expect(cite.cite.title).toEqual('Signal & Image Processing & More');
expect(cite.cite['container-title']).toEqual('Computers & Geosciences');
});
});

describe('firstNonDoiUrl', () => {
it('no url returns undefined', async () => {
expect(firstNonDoiUrl('my citation', 'abc123')).toEqual(undefined);
Expand Down