From 2be83d9834fa7a4e70adee743150133571515e13 Mon Sep 17 00:00:00 2001 From: Peter Blommendaal Date: Tue, 29 Oct 2024 22:38:25 +0100 Subject: [PATCH 1/3] Apply newline replacement in renderPattern to support multi-line image code blocks in reStructuredText (RST) files. --- src/config.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/config.js b/src/config.js index ac85022..807d1de 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); From c31d6debe6c4f7c1e6c2c44d594ca2aea930a27d Mon Sep 17 00:00:00 2001 From: Peter Blommendaal Date: Wed, 30 Oct 2024 01:08:04 +0100 Subject: [PATCH 2/3] Improve regex pattern for capturing imagePath in convertPatternToReg for reStructuredText. --- src/config.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/config.js b/src/config.js index 807d1de..1f74a41 100644 --- a/src/config.js +++ b/src/config.js @@ -108,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(); From 353b979a08251967d1ef19be4f703adb09f1cedf Mon Sep 17 00:00:00 2001 From: Peter Blommendaal Date: Wed, 30 Oct 2024 01:08:35 +0100 Subject: [PATCH 3/3] Add test.rst for debugging purposes --- test/test.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 test/test.rst 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