Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Repeated http://doi.org/http://doi.org/... in some links #1150

Merged
merged 1 commit into from
Apr 23, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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