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