UNPKG

@mintlify/common

Version:

Commonly shared code within Mintlify

26 lines (25 loc) 1.12 kB
import { slugifyWithCounter } from '@sindresorhus/slugify'; import { getUnicodeId } from './mdx/lib/remark-utils.js'; // decodes percent-encoded slugs and strips punctuation + emoji so the // resulting ID matches what the client renders in the DOM export const cleanHeadingId = (id) => { const decoded = decodeURIComponent(id); return decoded .replace(/[?,;:!'"()[\]{}]/g, '') .replace(/\p{Emoji_Modifier}|\p{Emoji_Modifier_Base}|\p{Emoji_Presentation}|\p{Extended_Pictographic}|[\u200D\uFE0E\uFE0F]/gu, ''); }; export function slugify(string, slugify = slugifyWithCounter()) { const encodedString = getUnicodeId(string); // if encoded title is already percent-encoded, return it as is // slugify doesn't support percent-encoded characters, like Chinese, Korean, etc. if (/%[0-9A-F]{2}/.test(encodedString)) { return slugify(encodedString, { decamelize: false, preserveCharacters: ['%', '_'], lowercase: false, }); } else { return slugify(encodedString, { decamelize: false, preserveCharacters: ['_'] }); } }