From 4156e83f57c4accf7b563c89b25fa09835d56964 Mon Sep 17 00:00:00 2001 From: infinite-persistence Date: Mon, 2 Aug 2021 22:10:49 +0800 Subject: [PATCH] Fix: OG messed up if `$"` is in title ## Issue 5951: OG messed up if `$"` is in title The replacement string would contain `...$"...`. The character `$&` has special meaning in `replace`, so the output was messed up. ## Approach Use a direct slice approach. A bit less elegant. --- web/src/html.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/web/src/html.js b/web/src/html.js index 8463180a2..508819834 100644 --- a/web/src/html.js +++ b/web/src/html.js @@ -23,13 +23,17 @@ const { getJsBundleId } = require('../bundle-id.js'); const jsBundleId = getJsBundleId(); function insertToHead(fullHtml, htmlToInsert) { - return fullHtml.replace( - /.*/s, - ` - ${htmlToInsert || buildOgMetadata()} - - ` - ); + const beginStr = ''; + const finalStr = ''; + + const beginIndex = fullHtml.indexOf(beginStr); + const finalIndex = fullHtml.indexOf(finalStr); + + if (beginIndex > -1 && finalIndex > -1 && finalIndex > beginIndex) { + return `${fullHtml.slice(0, beginIndex)}${ + htmlToInsert || buildOgMetadata() + }${fullHtml.slice(finalIndex + finalStr.length)}`; + } } function truncateDescription(description) {