Convert filename to a slug that can be used inside a URL. Replace spaces with dashes, convert characters to lowercase and remove file extension. Insert slug values inside Sublime Text with a snippet
1<snippet>
2 <content><![CDATA[
3${1:${TM_FILENAME/([A-Z])|(\s+)|(\.[^.]+$)/(?1\l$1:)(?2-:)(?3:)/gi}}
4]]></content>
5 <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
6 <tabTrigger>slug</tabTrigger>
7 <!-- Optional: Set a scope to limit where the snippet will trigger -->
8 <!-- <scope>source.markdown</scope> -->
9 <!-- Optional: Description to show in the menu -->
10 <description>Convert filename to a slug that can be used inside a URL. Replace spaces with dashes, convert characters to lowercase and remove file extension</description>
11</snippet>
Results:
ai-image-generation.md -> ai-image-generation
Online card payments.md -> online-card-payments
some Random FIle NAME with blah.txt -> some-random-file-name-with-blah
Usage:
Type slugand press Tab
Packages/User folder for you.sublime-snippetExplanation for
1${1:${TM_FILENAME/([A-Z])|(\s+)|(\.[^.]+$)/(?1\l$1:)(?2-:)(?3:)/g}}
$1 is a field marker, it means first position for your cursor${1:$TM_FULLNAME} means you’re using the variable$TM_FULLNAME as the default value for field marker $1${TM_FILENAME} is an environment variable that gives you the file name${TM_FILENAME/find/replace/g}()| means OR. (A)|(B)|(C) would match for group (A) or (B) or (C)$N. For example $1 would mean (A), $2 would mean (B) and $3 would mean (C) and so on/(A)|(B)|(C)/($1)($2)($3)/ would equal the original string(?1\l$1:) goes like this:1 matches, replace with \l$1 (a lowercase version of group 1 match), else with nothing.(?1'THIS':'THAT')?1 if group 1 is matched$1contents of group 1 matchtruthy:falsy. truthy will be executed if group matched, falsy will be executed if no match for that group was found(?{3}-:@) - if Group 3 matched, replace with -, else with @match groups:
([A-Z])matches all uppercase word characters, group 1(\s+) matches all whitespace characters, group 2(\.[^.]+$) matches file extension, group 3substitution groups:
(?1\l$1:) - if group 1 is matched, replace it with lowercase version of group 1(?2-:) - if group 2 is matched, replace it with -(?3:) - if group 3 is matched, replace it with nothing