Skip to content

Commit

Permalink
feat(nx-dev): remove emojis from heading ID
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Sep 15, 2023
1 parent 6242c87 commit 62aa63f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion nx-dev/feature-ai/src/lib/feed/feed-answer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface RehypeNode {
children?: RehypeNode[];
value?: string;
}

function openLinksInNewTab() {
function walk(node: RehypeNode, callback: (node: RehypeNode) => void): void {
callback(node);
Expand All @@ -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';
Expand Down
10 changes: 10 additions & 0 deletions nx-dev/ui-markdoc/src/lib/nodes/heading.schema.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
);
});
});
11 changes: 7 additions & 4 deletions nx-dev/ui-markdoc/src/lib/nodes/heading.schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RenderableTreeNode, Schema, Tag } from '@markdoc/markdoc';

function generateID(
export function generateID(
children: RenderableTreeNode[],
attributes: Record<string, any>
) {
Expand All @@ -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 = {
Expand Down

0 comments on commit 62aa63f

Please sign in to comment.