diff --git a/src/config.js b/src/config.js index ac85022..1f74a41 100644 --- a/src/config.js +++ b/src/config.js @@ -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); @@ -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(); diff --git a/test/test.rst b/test/test.rst new file mode 100644 index 0000000..b10ed67 --- /dev/null +++ b/test/test.rst @@ -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 \ No newline at end of file