Reputation: 7114
I want to create a VS Code snippet for Markdown files. The snippet should take the name of the file (e.g.: 12.3-Test_me.md
) and transform it to something like Test Me
with neither the leading numbers nor the extension, and with underscores replaced with a space.
However, I am failing to replace the underscore with a space.
The documentation for TextMate snippets promises:
We can also use conditional insertions in the format string to make decisions.
The example of conditional insertion given is
${1/void$|(.+)/(?1:\n\treturn nil;)/}
Are conditional insertions possible in VS Code? All my attempts to add one lead to the RegExp in my snippet failing. I just want to check if capture group 4 contains any characters, in which case I want to replace it with a space so in the third part of my snippet, I tried using:
${4/(.+)/(?1: )/}
Below is the snippet that I am using to understand what is going on. The first part shows how the Regular Expression is applied, and the second part is my best attempt so far at generating the output I want. The third part shows my failed attempt to use a conditional insertion.
"test": {
"prefix": "xxx",
"body": [
"Input: $TM_FILENAME\nTreatment:\n\n${TM_FILENAME/(^[0-9 _\\.-]+)|((\\w+)([ _-]))|(\\w+)|(\\.md$)/1: '$1'\n2: '$2'\n3: '$3'\n4: '$4'\n5: '${5:/capitalize}'\n6: '$6'\n\n/g}------\n",
"Output:\n'${TM_FILENAME/(^[0-9 _\\.-]+)|((\\w+)([ _-]))|(\\w+)|(\\.md$)/$3${4: }${5:/capitalize}/g}'\n(expected: 'Test Me')\n\n------\n",
"Fails:\n'${TM_FILENAME/(^[0-9 _\\.-]+)|((\\w+)([ _-]))|(\\w+)|(\\.md$)/$3${4/(.+)/(?1: )/}${5:/capitalize}/g}"
],
"description": "Create title from file name"
}
Here is what this produces when I apply the snippet to a file named 12.3-Test_me.md
:
Input: 12.3-Test_me.md
Treatment:
1: '12.3-'
2: ''
3: ''
4: ''
5: ''
6: ''
1: ''
2: 'Test_'
3: 'Test'
4: '_'
5: ''
6: ''
1: ''
2: ''
3: ''
4: ''
5: 'Me'
6: ''
1: ''
2: ''
3: ''
4: ''
5: ''
6: '.md'
------
Output:
' Test_ Me '
(expected: 'Test Me')
------
Fails:
'${TM_FILENAME/(^[0-9 _\.-]+)|((\w+)([ _-]))|(\w+)|(\.md$)/ (?1: )/}/capitalize/g}
Upvotes: 0
Views: 24
Reputation: 75
The following snippet does the following.
Input: 12.3-Test_me.md
Output: Test Me
Snippet:
"transform2":{
"prefix": "transform2"
,"body": ["${CLIPBOARD/(\\d*\\.*\\d-)(\\w*)_(\\w*)(\\.\\w*)/${2} ${3:/capitalize}/g}"]
,"description": ""
}
Details:
I'm using the CLIPBOARD
variable instead of TM_FILENAME
just for ease of testing.
This way I can test the snippet by copying the Input string and then triggering the trasnform2
snippet, instead of having to deal with filenames.
But you can just swap TM_FILENAME
in for CLIPBOARD
and it will use the filename instead of the text that is in your clipboard.
This snippet breaks up the input string into 4 groupings.
(\\d*\\.*\\d-)
=> "12.3-"(\\w*)
=> "Test"_(\\w*)
=> "me"
(\\.\\w*)
=> ".md"Now that we have the string split up into those 4 groupings we can write the "format string" (or replacement string) part, which is what comes after the second /
Format string: ${2} ${3:/capitalize}
${2}
=> "Test"${3:/capitalize}
=> "Me"
/capitalize
capitalizes the first letter. Turns the "me" to "Me".Note:
The literal space character between the ${2} and the ${3}.
I think the main part to understand in this case is that while we are using the _
to help capture the groupings before and after the _
.
In Test_me
The actual _
character is not part of group2 nor group3, because we leave it outside of the (
parentheses )
that designate the grouped texted.
You might still have to edit this further to get what you need for your specific use case, but hopefully this helps explain the main issue you were having with the _
being included in the output string and the extra spaces at the start and the end of your output string.
Upvotes: 0