Path: blob/main/extensions/markdown-language-features/src/slugify.ts
3291 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45export class Slug {6public constructor(7public readonly value: string8) { }910public equals(other: Slug): boolean {11return this.value === other.value;12}13}1415export interface Slugifier {16fromHeading(heading: string): Slug;17}1819export const githubSlugifier: Slugifier = new class implements Slugifier {20fromHeading(heading: string): Slug {21const slugifiedHeading = encodeURI(22heading.trim()23.toLowerCase()24.replace(/\s+/g, '-') // Replace whitespace with -25// allow-any-unicode-next-line26.replace(/[\]\[\!\/\'\"\#\$\%\&\(\)\*\+\,\.\/\:\;\<\=\>\?\@\\\^\{\|\}\~\`。,、;:?!…—·ˉ¨‘’“”々~‖∶"'`|〃〔〕〈〉《》「」『』.〖〗【】()[]{}]/g, '') // Remove known punctuators27.replace(/^\-+/, '') // Remove leading -28.replace(/\-+$/, '') // Remove trailing -29);30return new Slug(slugifiedHeading);31}32};333435