11import * as vscode from 'vscode' ;
22import { basename , dirname , isAbsolute , join , normalize , relative } from 'path' ;
33import { existsSync , writeFileSync } from 'fs' ;
4+ const GithubSlugger = require ( 'github-slugger' ) ;
5+ const SLUGGER = new GithubSlugger ( ) ;
46
57export 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