Skip to content

Commit 5394582

Browse files
committed
polyfill replaceAll so that RStudio Desktop preview pane works
1 parent 020b10b commit 5394582

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/resources/formats/html/mermaid/mermaid-init.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/**
2+
* String.prototype.replaceAll() polyfill
3+
* https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/
4+
* @author Chris Ferdinandi
5+
* @license MIT
6+
*/
7+
if (!String.prototype.replaceAll) {
8+
String.prototype.replaceAll = function (str, newStr) {
9+
// If a regex pattern
10+
if (
11+
Object.prototype.toString.call(str).toLowerCase() === "[object regexp]"
12+
) {
13+
return this.replace(str, newStr);
14+
}
15+
16+
// If a string
17+
return this.replace(new RegExp(str, "g"), newStr);
18+
};
19+
}
20+
121
mermaid.initialize({ startOnLoad: false });
222

323
const _quartoMermaid = {

0 commit comments

Comments
 (0)