Skip to content

Commit 4326868

Browse files
authored
Merge pull request #85 from kortina/ak-github-slugger
add github-slugger option
2 parents 6fe9032 + f572a85 commit 4326868

File tree

5 files changed

+288
-195
lines changed

5 files changed

+288
-195
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"basenames",
2828
"davovscapcom",
2929
"digiguru",
30+
"dont",
3031
"filepath",
3132
"fullwidth",
3233
"jumplist",

package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@
8585
"default": "-",
8686
"description": "When creating new notes from a 'Title Case Note Name', slugify non-word characters with '-' (default) or '_', or don't slugify non-word characters by setting to 'NONE.'"
8787
},
88+
"vscodeMarkdownNotes.slugifyMethod": {
89+
"type": "string",
90+
"enum": [
91+
"classic",
92+
"github-slugger"
93+
],
94+
"default": "classic",
95+
"description": "Method converting 'Title Case Note Names' to 'slug-file-names.' Defaults to 'classic.' See unit tests in vscode-markdown-notes GitHub project for more details."
96+
},
8897
"vscodeMarkdownNotes.defaultFileExtension": {
8998
"type": "string",
9099
"default": "md",
@@ -178,11 +187,15 @@
178187
"vscode:prepublish": "npm run compile",
179188
"watch": "tsc -watch -p ./"
180189
},
190+
"dependencies": {
191+
"github-slugger": "^1.3.0"
192+
},
181193
"devDependencies": {
182194
"@babel/core": "^7.10.1",
183195
"@babel/preset-env": "^7.10.1",
184196
"@babel/preset-typescript": "^7.10.1",
185197
"babel-jest": "^26.0.1",
198+
"@types/github-slugger": "^1.3.0",
186199
"@types/glob": "^7.1.1",
187200
"@types/jest": "^25.2.3",
188201
"@types/jest-cli": "^24.3.0",

src/NoteWorkspace.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as vscode from 'vscode';
22
import { basename, dirname, isAbsolute, join, normalize, relative } from 'path';
33
import { existsSync, writeFileSync } from 'fs';
4+
const GithubSlugger = require('github-slugger');
5+
const SLUGGER = new GithubSlugger();
46

57
export const foo = () => {
68
return 1;
@@ -23,7 +25,12 @@ enum SlugifyCharacter {
2325
none = 'NONE',
2426
}
2527

26-
enum PipedWikiLinksSyntax {
28+
export enum SlugifyMethod {
29+
github = 'github-slugger',
30+
classic = 'classic',
31+
}
32+
33+
export enum PipedWikiLinksSyntax {
2734
fileDesc = 'file|desc',
2835
descFile = 'desc|file',
2936
}
@@ -39,6 +46,7 @@ type Config = {
3946
defaultFileExtension: string;
4047
noteCompletionConvention: NoteCompletionConvention;
4148
slugifyCharacter: SlugifyCharacter;
49+
slugifyMethod: SlugifyMethod;
4250
workspaceFilenameConvention: WorkspaceFilenameConvention;
4351
newNoteTemplate: string;
4452
compileSuggestionDetails: boolean;
@@ -63,25 +71,21 @@ export class NoteWorkspace {
6371
static _rxTitle = '(?<=^( {0,3}#[^\\S\\r\\n]+)).+';
6472
static _rxMarkdownWordPattern = '([\\_\\w\\#\\.\\/\\\\]+)'; // had to add [".", "/", "\"] to get relative path completion working and ["#"] to get tag completion working
6573
static _rxFileExtensions = '\\.(md|markdown|mdx|fountain)$';
66-
static _defaultFileExtension = 'md';
67-
static _defaultNoteTemplate = '# ${noteName}\n\n';
68-
static _defaultTriggerSuggestOnReplacement = true;
6974
static SLUGIFY_NONE = 'NONE';
7075
static NEW_NOTE_SAME_AS_ACTIVE_NOTE = 'SAME_AS_ACTIVE_NOTE';
7176
static NEW_NOTE_WORKSPACE_ROOT = 'WORKSPACE_ROOT';
72-
static _defaultSlugifyChar = '-';
73-
static _slugifyChar = '-';
7477
static DEFAULT_CONFIG: Config = {
7578
createNoteOnGoToDefinitionWhenMissing: true,
7679
compileSuggestionDetails: false,
77-
defaultFileExtension: NoteWorkspace._defaultFileExtension,
80+
defaultFileExtension: 'md',
7881
noteCompletionConvention: NoteCompletionConvention.rawFilename,
7982
slugifyCharacter: SlugifyCharacter.dash,
83+
slugifyMethod: SlugifyMethod.classic,
8084
workspaceFilenameConvention: WorkspaceFilenameConvention.uniqueFilenames,
81-
newNoteTemplate: NoteWorkspace._defaultNoteTemplate,
82-
triggerSuggestOnReplacement: NoteWorkspace._defaultTriggerSuggestOnReplacement,
85+
newNoteTemplate: '# ${noteName}\n\n',
86+
triggerSuggestOnReplacement: true,
8387
allowPipedWikiLinks: false,
84-
pipedWikiLinksSyntax: PipedWikiLinksSyntax.descFile,
88+
pipedWikiLinksSyntax: PipedWikiLinksSyntax.fileDesc,
8589
pipedWikiLinksSeparator: '\\|',
8690
newNoteDirectory: NoteWorkspace.NEW_NOTE_SAME_AS_ACTIVE_NOTE,
8791
previewLabelStyling: PreviewLabelStyling.brackets,
@@ -106,6 +110,7 @@ export class NoteWorkspace {
106110
defaultFileExtension: c.get('defaultFileExtension') as string,
107111
noteCompletionConvention: c.get('noteCompletionConvention') as NoteCompletionConvention,
108112
slugifyCharacter: c.get('slugifyCharacter') as SlugifyCharacter,
113+
slugifyMethod: c.get('slugifyMethod') as SlugifyMethod,
109114
workspaceFilenameConvention: c.get(
110115
'workspaceFilenameConvention'
111116
) as WorkspaceFilenameConvention,
@@ -125,6 +130,10 @@ export class NoteWorkspace {
125130
return this.cfg().slugifyCharacter;
126131
}
127132

133+
static slugifyMethod(): string {
134+
return this.cfg().slugifyMethod;
135+
}
136+
128137
static defaultFileExtension(): string {
129138
return this.cfg().defaultFileExtension;
130139
}
@@ -283,7 +292,9 @@ export class NoteWorkspace {
283292
}
284293

285294
// Compare 2 wiki-links for a fuzzy match.
286-
// All of the following will return true
295+
// In general, we expect
296+
// `left` to be fsPath
297+
// `right` to be the ref word [[wiki-link]]
287298
static noteNamesFuzzyMatch(left: string, right: string): boolean {
288299
return (
289300
this.normalizeNoteNameForFuzzyMatch(left).toLowerCase() ==
@@ -303,14 +314,28 @@ export class NoteWorkspace {
303314
.toLowerCase() // lower
304315
.replace(/[-__ ]*$/g, ''); // removing trailing slug chars
305316
}
306-
static slugifyTitle(title: string): string {
317+
318+
static slugifyClassic(title: string): string {
307319
let t =
308320
this.slugifyChar() == 'NONE'
309321
? title
310322
: title.replace(/[!"\#$%&'()*+,\-./:;<=>?@\[\\\]^_{|}~\s]+/gi, this.slugifyChar()); // punctuation and whitespace to hyphens (or underscores)
311323
return this.cleanTitle(t);
312324
}
313325

326+
static slugifyGithub(title: string): string {
327+
SLUGGER.reset(); // otherwise it will increment repeats with -1 -2 -3 etc.
328+
return SLUGGER.slug(title);
329+
}
330+
331+
static slugifyTitle(title: string): string {
332+
if (this.slugifyMethod() == SlugifyMethod.classic) {
333+
return this.slugifyClassic(title);
334+
} else {
335+
return this.slugifyGithub(title);
336+
}
337+
}
338+
314339
static noteFileNameFromTitle(title: string): string {
315340
let t = this.slugifyTitle(title);
316341
return t.match(this.rxFileExtensions()) ? t : `${t}.${this.defaultFileExtension()}`;

0 commit comments

Comments
 (0)