From 3aa40b03d378b6157d744774620c297bd4ddb44a Mon Sep 17 00:00:00 2001 From: Rowan Cockett Date: Thu, 28 Nov 2024 15:54:32 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=97=20Improve=20link=20formatting=20(#?= =?UTF-8?q?1684)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit No hanging single characters on a link --- .changeset/nervous-avocados-tell.md | 5 +++++ .../myst-transforms/src/links/plugin.spec.ts | 20 ++++++++++++++++++- packages/myst-transforms/src/links/plugin.ts | 6 ++++-- 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 .changeset/nervous-avocados-tell.md diff --git a/.changeset/nervous-avocados-tell.md b/.changeset/nervous-avocados-tell.md new file mode 100644 index 000000000..cca0d8995 --- /dev/null +++ b/.changeset/nervous-avocados-tell.md @@ -0,0 +1,5 @@ +--- +"myst-transforms": patch +--- + +No trailing slash for end of links diff --git a/packages/myst-transforms/src/links/plugin.spec.ts b/packages/myst-transforms/src/links/plugin.spec.ts index 4c21e9734..e0901c09a 100644 --- a/packages/myst-transforms/src/links/plugin.spec.ts +++ b/packages/myst-transforms/src/links/plugin.spec.ts @@ -3,7 +3,7 @@ import { VFile } from 'vfile'; import type { Link } from 'myst-spec-ext'; import type { ResolvedExternalReference } from './types'; import { MystTransformer } from './myst'; -import { linksTransform } from './plugin'; +import { formatLinkText, linksTransform } from './plugin'; export const TEST_REFERENCES: ResolvedExternalReference[] = [ { @@ -47,3 +47,21 @@ describe('Link Plugin Transformer', () => { expect(link.children).toEqual([]); }); }); + +describe('formatLinkText', () => { + test.each([ + ['https://mystmd.com/guide', 'https://​mystmd​.com​/guide'], + ['https://mystmd.com/guide#x', 'https://​mystmd​.com​/guide#x'], + ['https://mystmd.com/guide#abc', 'https://​mystmd​.com​/guide​#abc'], + ['https://mystmd.com/guide/', 'https://​mystmd​.com​/guide/'], + ['https://mystmd.com/guide/', 'https://​mystmd​.com​/guide/'], + [ + 'https://mystmd.com/guide/citaion-format?test=1#ok', + 'https://​mystmd​.com​/guide​/citaion​-format​?test​=​1​#ok', + ], + ])('Link Text — %s', (url, result) => { + const node = { type: 'link', children: [{ type: 'text', value: url }] }; + formatLinkText(node as any); + expect(node.children[0].value).toBe(result); + }); +}); diff --git a/packages/myst-transforms/src/links/plugin.ts b/packages/myst-transforms/src/links/plugin.ts index 10b9236d6..92a61c0e3 100644 --- a/packages/myst-transforms/src/links/plugin.ts +++ b/packages/myst-transforms/src/links/plugin.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-irregular-whitespace */ import type { Plugin } from 'unified'; import { selectAll } from 'unist-util-select'; import type { VFile } from 'vfile'; @@ -15,7 +16,7 @@ type Options = { * Uses a zero-width space, same as a `` but no fancy rendering required. * https://css-tricks.com/better-line-breaks-for-long-urls/ */ -function formatLinkText(link: Link) { +export function formatLinkText(link: Link) { if (link.children?.length !== 1 || link.children[0].type !== 'text') return; const url = link.children[0].value; // Add an exception for wiki transforms links. @@ -34,7 +35,8 @@ function formatLinkText(link: Link) { .replace(/([=&])/giu, '​$1​'), // Reconnect the strings with word break opportunities after double slashes ) - .join('//​'); + .join('//​') + .replace(/​(.{1,2})$/, '$1'); link.children[0].value = formatted; }