Skip to content

Commit

Permalink
Properly handling <br> tags
Browse files Browse the repository at this point in the history
  • Loading branch information
daladim committed Dec 1, 2020
1 parent c117ee2 commit 3b8fee7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
12 changes: 12 additions & 0 deletions run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
INPUT_DATA=$1
IMAGETAG=$2

set -e

# Test if the CLI actually works (SVG, then check the SVG is valid and properly handled by standard tools)
for i in $(ls $INPUT_DATA/*.mmd); do
docker run -v $(pwd):/data $IMAGETAG -i /data/$i -o /data/$i.svg
docker run -v $(pwd):/data flungo/inkscape /usr/bin/inkscape -e /data/$i-converted.png /data/$i.svg
done
for i in $(ls $INPUT_DATA/*.mmd); do
cat $i | docker run -i -v $(pwd):/data $IMAGETAG -o /data/$i-stdin.svg -w 800;
docker run -v $(pwd):/data flungo/inkscape /usr/bin/inkscape -e /data/$i-converted.png /data/$i-stdin.svg
done

# Test if the CLI actually works (PNG)
for i in $(ls $INPUT_DATA/*.mmd); do docker run -v $(pwd):/data $IMAGETAG -i /data/$i -o /data/$i.png -w 800; done
for i in $(ls $INPUT_DATA/*.mmd); do cat $i | docker run -i -v $(pwd):/data $IMAGETAG -o /data/$i-stdin.png -w 800; done
Expand Down
14 changes: 13 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ const getInputData = async inputFile => new Promise((resolve, reject) => {
})
})

const convertToValidXML = html => {
xml = html

// <br> tags in valid HTML (from innerHTML) look like <br>, but they must look like <br/> to be valid XML (such as SVG)
xml.replace(/<br>/gi, '<br/>')

return xml
}



commander
.version(pkg.version)
.option('-t, --theme [theme]', 'Theme of the chart, could be default, forest, dark or neutral. Optional. Default: default', /^default|forest|dark|neutral$/, 'default')
Expand Down Expand Up @@ -156,7 +167,8 @@ const deviceScaleFactor = parseInt(scale || 1, 10);

if (output.endsWith('svg')) {
const svg = await page.$eval('#container', container => container.innerHTML)
fs.writeFileSync(output, svg)
svg_xml = convertToValidXML(svg)
fs.writeFileSync(output, svg_xml)
} else if (output.endsWith('png')) {
const clip = await page.$eval('svg', svg => {
const react = svg.getBoundingClientRect()
Expand Down
4 changes: 4 additions & 0 deletions test-positive/graph-with-br.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
graph TD
subgraph sub
node(Line 1<br>Line 2<br/>Line 3)
end

0 comments on commit 3b8fee7

Please sign in to comment.