Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ class Config {
const matchedPattern = this.renderMap.find((item) => {
return minimatch(filePath, item.matchRule);
});
return matchedPattern.renderPattern ?? this.renderPatternDeprecated;
}

// Apply the replacement so that \n is converted to a real newline
const pattern = matchedPattern?.renderPattern ?? this.renderPatternDeprecated;
return pattern.replace(/\\n/g, '\n');
}

get matchPattern() {
return convertPatternToReg(this.renderPattern);
Expand Down Expand Up @@ -105,13 +108,23 @@ function getRenderPattern() {
}

function convertPatternToReg(pattern) {
// Define a regular expression to escape special regex characters
const regOp = /[|\\{}()[\]^$+*?.]/g;
const excapedPattern = pattern.replace(regOp, "\\$&");
const matchPattern = excapedPattern.replace(

// Escape special characters in the pattern, so it can be safely converted to a regex
const escapedPattern = pattern.replace(regOp, "\\$&");

// Replace placeholders like ${imagePath} with named capturing groups in the regex.
// Using `.*` (instead of non-greedy `.*?`) allows the match to extend to the end of the line,
// ensuring that paths or other elements are fully captured even if they are longer.
const matchPattern = escapedPattern.replace(
/\\\$\\\{(.*?)\\\}/g,
(match, p1) => `(?<${p1}>.*?)` // group name
);
(match, p1) => `(?<${p1}>.*)` // Greedy match to capture complete path
).replace(/image::\s*/, "image::\\s*"); // Make space after "image::" optional

// Return the constructed regular expression
return new RegExp(matchPattern, "g");
}


module.exports = new Config();
3 changes: 3 additions & 0 deletions test/test.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. image:: assets/2024-10-29-23-05-13.png
:class: browser-screenshot with-shadow
:alt: assets/2024-10-29-23-05-13.png