Skip to content

Commit 9074a7a

Browse files
authored
Merge pull request #149 from lagthedark/fix-image-path
Added regex capture groups support for slug in targetPath and linkPattern
2 parents e703cce + 6ee0b2d commit 9074a7a

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,20 @@ Smartly paste for Markdown.
325325
}
326326
]
327327
```
328+
329+
You can also use **regex capture groups** in your `match` patterns. Each captured group can be referenced in `targetPath` and `linkPattern` as `$1`, `$2`, etc. This allows you to dynamically create folders or links based on parts of the Markdown filename or path.
330+
331+
**Example:**
332+
333+
```json
334+
"MarkdownPaste.imageRules": [
335+
{
336+
"match": "(.*)-example\\.md$",
337+
"targetPath": "${workspaceFolder}/images/$1/image.png",
338+
"linkPattern": "![${altText}](../images/$1/image.png)"
339+
}
340+
]
341+
```
328342

329343
## Issues and Suggestions
330344

package-lock.json

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

src/paster.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,18 +202,32 @@ class Paster {
202202
for (const rule of rules) {
203203
if (rule.match) {
204204
const re = new RegExp(rule.match, rule.options || "");
205-
if (re.test(currentFilePath)) {
206-
// Generate all variables (including timestamp) here
207-
const processedRule = { ...rule };
205+
const match = re.exec(currentFilePath);
206+
if (match) {
207+
let processedRule = { ...rule };
208208
if (processedRule.targetPath) {
209209
processedRule.targetPath = Predefine.replacePredefinedVars(
210210
processedRule.targetPath
211211
);
212+
match.forEach((value, index) => {
213+
if (index === 0) return;
214+
processedRule.targetPath = processedRule.targetPath.replaceAll(
215+
`$${index}`,
216+
value
217+
);
218+
});
212219
}
213220
if (processedRule.linkPattern) {
214221
processedRule.linkPattern = Predefine.replacePredefinedVars(
215222
processedRule.linkPattern
216223
);
224+
match.forEach((value, index) => {
225+
if (index === 0) return;
226+
processedRule.linkPattern = processedRule.linkPattern.replaceAll(
227+
`$${index}`,
228+
value
229+
);
230+
});
217231
}
218232
return processedRule;
219233
}

0 commit comments

Comments
 (0)