From 62aa63fb4e7293620a3df33268333421b4d535b2 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Fri, 15 Sep 2023 11:48:20 -0400 Subject: [PATCH] feat(nx-dev): remove emojis from heading ID --- nx-dev/feature-ai/src/lib/feed/feed-answer.tsx | 2 +- .../ui-markdoc/src/lib/nodes/heading.schema.spec.ts | 10 ++++++++++ nx-dev/ui-markdoc/src/lib/nodes/heading.schema.ts | 11 +++++++---- 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 nx-dev/ui-markdoc/src/lib/nodes/heading.schema.spec.ts diff --git a/nx-dev/feature-ai/src/lib/feed/feed-answer.tsx b/nx-dev/feature-ai/src/lib/feed/feed-answer.tsx index da3a664442528..54172093819af 100644 --- a/nx-dev/feature-ai/src/lib/feed/feed-answer.tsx +++ b/nx-dev/feature-ai/src/lib/feed/feed-answer.tsx @@ -19,6 +19,7 @@ interface RehypeNode { children?: RehypeNode[]; value?: string; } + function openLinksInNewTab() { function walk(node: RehypeNode, callback: (node: RehypeNode) => void): void { callback(node); @@ -30,7 +31,6 @@ function openLinksInNewTab() { return (tree: RehypeNode) => { walk(tree, (node) => { if (node.type === 'element' && node.tagName === 'a') { - console.log(node); const props = node.properties ?? {}; const href = props?.['href'] as string; props.target = '_blank'; diff --git a/nx-dev/ui-markdoc/src/lib/nodes/heading.schema.spec.ts b/nx-dev/ui-markdoc/src/lib/nodes/heading.schema.spec.ts new file mode 100644 index 0000000000000..e4f2172f7504c --- /dev/null +++ b/nx-dev/ui-markdoc/src/lib/nodes/heading.schema.spec.ts @@ -0,0 +1,10 @@ +import { generateID } from './heading.schema'; + +describe('heading schema: generateID', () => { + it('should create ID with ony ASCII characters', () => { + expect(generateID(['Hello', 'World'], {})).toEqual('hello-world'); + expect(generateID(['🎉 Pro: Simple Setup'], {})).toEqual( + 'pro-simple-setup' + ); + }); +}); diff --git a/nx-dev/ui-markdoc/src/lib/nodes/heading.schema.ts b/nx-dev/ui-markdoc/src/lib/nodes/heading.schema.ts index 9df6c8873a0f3..0fda4e70748a4 100644 --- a/nx-dev/ui-markdoc/src/lib/nodes/heading.schema.ts +++ b/nx-dev/ui-markdoc/src/lib/nodes/heading.schema.ts @@ -1,6 +1,6 @@ import { RenderableTreeNode, Schema, Tag } from '@markdoc/markdoc'; -function generateID( +export function generateID( children: RenderableTreeNode[], attributes: Record ) { @@ -10,9 +10,12 @@ function generateID( return children .filter((child) => typeof child === 'string') .join(' ') - .replace(/[?]/g, '') - .replace(/\s+/g, '-') - .toLowerCase(); + .normalize('NFD') + .replace(/[\u0300-\u036f]/g, '') + .toLowerCase() + .replace(/[^a-z0-9 ]/g, '') + .trim() + .replace(/\s+/g, '-'); } export const heading: Schema = {