问题描述
我正在尝试制作一个片段,它将获取剪贴板内容(markdown
文档中标题的文本)并将其转换为指向该部分的链接.例如,如果我的剪贴板包含:Some Heading - 20191107
那么我希望输出以下内容:
I am trying to make a snippet that will take clipboard contents (the text of a heading in a markdown
document) and transform it into a link to that section. For example, if my clipboard contains: Some Heading - 20191107
then I want the following to be output:
[Some Heading - 20191107](filename.md#some-heading---20191107)
这是我目前用于 markdown
的 VS Code
片段:
Here is my snippet VS Code
for markdown
so far:
"link to this section": {
"prefix": "isection",
"body": [
"[${1:${CLIPBOARD}}](${TM_FILENAME}#${CLIPBOARD/ /-/g})"
],
"description": "Insert link to section whose heading text is in the clipboard"
}
这是第一个转换,但我不知道如何嵌套多个转换:
This has the first transform, but what I cannot figure out is how to nest multiple transforms:
- 用连字符替换所有空格.
- 全部改为小写.
- 删除任何匹配
[^a-z0-9-]
的字符
为了澄清我对@Mark 的测试用例,在 VS Code
的 markdown
文档中,我做了一个部分标题,例如:
To clarify my test case for @Mark, in a markdown
document in VS Code
, I make a section heading such as:
# 20191107 - @#$%^& This is a section - 20191107
然后我复制文本 20191107 - @#$%^&这是一个部分 - 20191107
并运行您为我修复的代码段.它输出的是:
I then copy the text 20191107 - @#$%^& This is a section - 20191107
and run the snippet you fixed up for me. What it outputs is:
[20191107 - @#$%^& This is a section - 20191107](tips.tech.git.md#20191107----this-is-a-section---20191107)
哪个是标题的有效链接!
Which is a valid link to the heading!
推荐答案
这是一个我认为满足所有要求的片段(我从我之前的回答中简化了这个).
Here is a snippet that I believe meets all requirements (I have simplified this from an earlier answer of mine).
"link to this section": {
"prefix": "isection",
"body": [
"[${1:${CLIPBOARD}}](${TM_FILENAME}#${CLIPBOARD/([\\w-]+$)|([\\w-]+)|([-\\s]+)|([^\\w]+)/${1:/downcase}${2:/downcase}${2:+-}/gm})"
],
"description": "Insert link to section whose heading text is in the clipboard"
}
我会解释这部分:
${CLIPBOARD/([\\w-]+$)|([\\w-]+)|([-\\s]+)|([^\\w]+)/${1:/downcase}${2:/downcase}${2:+-}/gm}
这里的主要思想是捕获每个组以在其自己的组中进行不同的处理.正则表达式交替只会为每场比赛捕获一组.请参阅 regex101 演示.
The main idea here is to capture each group to be handled differently in its own group. A regex alternation will just capture one group for each match. See regex101 demo.
然后您可以转换该组或忽略它,而不会影响任何后续匹配!
Then you can transform that group or ignore it without affecting any subsequent matches!
是四个捕获组的交替:
([\\w-]+$)
注意$
表示行尾,必须是第一个捕获组([\\w-]+)
与第 1 组相同,但不在行尾([-\\s]+)
捕获组中的空格和连字符([^\\w]+)
捕获组中除A-Za-z0-9
之外的任何字符
([\\w-]+$)
note the$
to indicate end of line, must be first capture group([\\w-]+)
same as group 1, but not at end of line([-\\s]+)
capture spaces and hyphens in a group([^\\w]+)
capture any characters other thanA-Za-z0-9
in a group
捕获组 1 获取最后 字符集,例如 12345
或 asdasd
.
Capture group 1 gets the last set of characters, like 12345
or asdasd
.
捕获组 2 获取与组 1 相同的字符组,但如果它们位于行尾则不会.这很重要,因为如果存在捕获组 2,则会添加 -
,但如果存在捕获组 1,则不会添加(因此不会在末尾添加连字符).
Capture group 2 gets the same groups of characters as group 1 but not if they are at the end of the line. This is important because a -
will be added if there is a capture group 2, but not if there is a capture group 1 (so no hyphen is added to the end).
捕获组 3 捕获空格和连字符.它将在输出中被忽略.
Capture group 3 captures the spaces and hyphens. It will be ignored in the output.
捕获组 4 捕获那些非 A-Za-z0-9
字符并将被忽略.
Capture group 4 captures those non-A-Za-z0-9
characters and will be ignored.
这是转换的输出:${1:/downcase}${2:/downcase}${2:+-}
注意没有提到第 3 组或第 4 组 -他们正在被丢弃.但是它们必须匹配,否则它们将通过未转换"并出现在结果中 - 我们没有.
Here is the output of the transform: ${1:/downcase}${2:/downcase}${2:+-}
notice there is no mention of groups 3 or 4 - they are being discarded. But they must be matched otherwise they will pass through "un-transformed" and appear in the result - which we do not.
所以小写组 1 和 2,由于交替,永远不会在同一场比赛中同时出现.
So lowercase groups 1 and 2, because of the alternation there will never be both in the same match.
${2:+-}
如果 有一个组 2 在它后面添加一个 -
.整个 CLIPBOARD 的最后一场比赛将是第 1 组,因此最后一场比赛不会附加连字符.
${2:+-}
if there is a group 2 add a -
after it. The very last match of the entire CLIPBOARD will be a group 1 so for this last match no hyphen will be appended.
由于 g
标志,正则表达式运行了几次,每次只捕获 4 个组中的一个.
Because of the g
flag the regex runs a few times, each time capturing only one of the 4 groups.
输入:Some Heading - 20191107
输出:[Some Heading - 20191107](fileName.ext#some-heading-20191107)
输入:20191107 - @#$%^&这是一个部分 - 20191107
输出:[20191107 - @#$%^&这是一个部分 - 20191107](test-bed-snippets.code-snippets#20191107-this-is-a-section-20191107)
如果结果中需要更多连字符,例如:
If you need more hyphens in the result, like:
[Some Heading - 20191107](filename.md#some-heading---20191107)
只需从第三个捕获组中取出连字符:([\\s]+)
得到:
just take the hyphen out of the third capture group: ([\\s]+)
to result in:
[20191107 - @#$%^&这是一个部分 - 20191107](test-bed-snippets.code-snippets#20191107---this-is-a-section---20191107)
这篇关于具有多个转换的 VS Code 片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!