Skip to content

Commit

Permalink
馃悰 Repeated http://doi.org/http://doi.org/...` in some links (#1150)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 committed Apr 23, 2024
1 parent 6e1ec9b commit f90882d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-turtles-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"citation-js-utils": patch
---

Avoid repeated `https://doi.org/https://doi.org/...`
3 changes: 2 additions & 1 deletion packages/citation-js-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Cite } 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';

Expand Down Expand Up @@ -121,7 +122,7 @@ export type CitationRenderer = Record<
>;

function doiUrl(doi?: string) {
return doi ? `https://doi.org/${doi}` : undefined;
return doi ? doiUtils.buildUrl(doi) : undefined;
}

function wrapWithAnchorTag(url: string, text?: string) {
Expand Down
1 change: 1 addition & 0 deletions packages/citation-js-utils/tests/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe('Test reference rendering', () => {
const data = parseBibTeX(src);
const citations = await getCitationRenderers(data);
expect(citations['cury2020sparse'].getDOI()).toBe(TEST_DOI_IN_OTHER_FIELD);
expect(citations['cury2020sparse'].getURL()).toBe(`https://doi.org/${TEST_DOI_IN_OTHER_FIELD}`);
});
});

Expand Down
9 changes: 4 additions & 5 deletions packages/myst-transforms/src/links/doi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { VFile } from 'vfile';
import type { Link, LinkTransformer } from './types.js';
import { updateLinkTextIfEmpty } from './utils.js';

const DOI_ORG = 'https://doi.org/';
const TRANSFORM_SOURCE = 'LinkTransform:DOITransformer';

export class DOITransformer implements LinkTransformer {
Expand All @@ -21,17 +20,17 @@ export class DOITransformer implements LinkTransformer {

transform(link: Link, file: VFile): boolean {
const urlSource = link.urlSource || link.url;
const doiString = doi.normalize(urlSource);
if (!doiString) {
const doiUrl = doi.buildUrl(urlSource);
if (!doiUrl) {
fileError(file, `DOI is not valid: ${urlSource}`, {
node: link,
source: TRANSFORM_SOURCE,
ruleId: RuleId.doiLinkValid,
});
return false;
}
link.url = `${DOI_ORG}${doiString}`;
link.data = { doi: doiString };
link.url = doiUrl;
link.data = { doi: doi.normalize(doiUrl) };
link.internal = false;
updateLinkTextIfEmpty(link, '');
return true;
Expand Down

0 comments on commit f90882d

Please sign in to comment.