Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
Fix: special string.replace sequences in Markdown (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelidlessness committed Sep 27, 2022
1 parent 4945f89 commit 8de64b5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,12 @@ function _renderMarkdown(htmlDoc, mediaMap) {
.forEach((key) => {
const replacement = replacements[key];
if (replacement) {
htmlStr = htmlStr.replace(key, replacement);
/**
* The replacement is called as a function here so special string
* replacement sequences are preserved if they appear within Markdown.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_the_replacement}
*/
htmlStr = htmlStr.replace(key, () => replacement);
}
});

Expand Down
35 changes: 35 additions & 0 deletions test/forms/md-str-replace-chars.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:jr="http://openrosa.org/javarosa"
xmlns:odk="http://www.opendatakit.org/xforms"
xmlns:orx="http://openrosa.org/xforms"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<h:head>
<h:title>md-str-replace-chars</h:title>
<model>
<itext>
<translation default="true()" lang="default">
<text id="special-chars:label">
<value>*$' is $` this $&amp; the $$ real $0 $&lt;life&gt;?*</value>
</text>
</translation>
</itext>
<instance>
<data id="rank">
<special-chars/>
<meta>
<instanceID/>
</meta>
</data>
</instance>
<bind nodeset="/data/special-chars"/>
</model>
</h:head>
<h:body>
<input ref="/data/special-chars">
<label ref="jr:itext('special-chars:label')" />
</input>
</h:body>
</h:html>
14 changes: 14 additions & 0 deletions test/transformer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,20 @@ describe('transformer', () => {
'formatted: <em><span class="or-output" data-value="/output/txt"> </span></em> and'
);
});

it('preserves text containing special string replacement sequences', async () => {
const xform = fs.readFileSync(
'./test/forms/md-str-replace-chars.xml',
'utf8'
);
const result = await transformer.transform({
xform,
});

expect(result.form).to.contain(
"<em>$' is $` this $&amp; the $$ real $0 $&lt;life&gt;?</em>"
);
});
});

describe('does not render markdown', () => {
Expand Down

0 comments on commit 8de64b5

Please sign in to comment.