Skip to content

Commit

Permalink
implement mermaid js
Browse files Browse the repository at this point in the history
  • Loading branch information
PrajwolAmatya committed Jan 21, 2025
1 parent 7013371 commit 2a98377
Show file tree
Hide file tree
Showing 10 changed files with 4,988 additions and 1,159 deletions.
11 changes: 11 additions & 0 deletions css/metadata.scss
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ $default-font-size: 22px;
display: flex;
flex-direction: column;
justify-content: center;

.code-wrapper {
box-shadow: none;
margin: 0;

.mermaid {
max-height: 100%;
background: #ffffff;
text-align: center;
}
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion index.example.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
updateImageUrl('{{{ imagePath }}}')
addCustomSlideNumber(event)
fitContent()
adjustFontSize()
showHideFooterAndSlideNumber('{{{ slideNumber }}}', {{ hideFooter }})
setFullPageBackground({{{ headingData }}}, '{{{ imagePath }}}', {{ config.pdfWidth }}, {{ config.pdfHeight }})
})
Expand All @@ -47,7 +48,7 @@
hash: true,

// Learn about plugins: https://revealjs.com/plugins/
plugins: [RevealAwesoMD, RevealHighlight, RevealNotes],
plugins: [RevealAwesoMD, RevealHighlight, RevealNotes, RevealMermaid],
})
</script>
</body>
Expand Down
1 change: 1 addition & 0 deletions js/vendor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const vendorScripts = [
'node_modules/reveal.js/plugin/notes/notes.js',
'node_modules/revealjs-awesomd/plugin/awesoMD/awesoMD.js',
'node_modules/reveal.js/plugin/highlight/highlight.js',
'node_modules/reveal.js-mermaid-plugin/plugin/mermaid/mermaid.js',
]

module.exports = vendorScripts
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"marked": "^4.3.0",
"puppeteer": "^23.1.0",
"reveal.js": "^5.1.0",
"reveal.js-mermaid-plugin": "^2.3.0",
"sass": "^1.77.0",
"vinyl-buffer": "^1.0.1",
"vinyl-source-stream": "^2.0.0",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6,080 changes: 4,926 additions & 1,154 deletions tests/unit/jest/__snapshots__/presentation.spec.js.snap

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions tests/unit/jest/presentation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ const getTotalSlides = async () => {
})
}

// mermaid js generates an unique id for every svg element (e.g. id="mermaid-8iido71fowf")
// this results in the snapshot test to fail
// so normalizing the svg element with static id
const normalizeSvgElement = async (pageContent) => {
return pageContent.replace(/mermaid-[\w-]+/g, 'mermaid-unique-id')
}

beforeAll(async () => {
await testHelper.copyAssets()
await startTestServer('test.md')
Expand Down Expand Up @@ -85,19 +92,24 @@ beforeEach(async () => {
describe('test markdown presentation', () => {
it('should match the total number of slides', async () => {
const totalSlides = await getTotalSlides()
expect(totalSlides).toEqual(33)
expect(totalSlides).toEqual(34)
})

it('should render markdown presentation', async () => {
await setScreenSize(screenWidth, screenHeight)
expect(beautify(await page.content())).toMatchSnapshot()
const verifySlideContent = async () => {
const pageContent = beautify(await page.content())
const normalizedSvgPageContent = await normalizeSvgElement(pageContent)
expect(normalizedSvgPageContent).toMatchSnapshot()
}
await verifySlideContent()

const totalSlides = await getTotalSlides()
for (let i = 0; i < totalSlides - 1; i++) {
const element = await page.waitForSelector('.navigate-right')
await element.click()
await testHelper.waitForTransitionEnd(page, '.progress')
expect(beautify(await page.content())).toMatchSnapshot()
await verifySlideContent()
}
}, 60000)

Expand Down
3 changes: 2 additions & 1 deletion tests/unit/testFiles/index.example.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
updateImageUrl('{{{ imagePath }}}')
addCustomSlideNumber(event)
fitContent()
adjustFontSize()
showHideFooterAndSlideNumber('{{{ slideNumber }}}', {{ hideFooter }})
setFullPageBackground({{{ headingData }}}, '{{{ imagePath }}}', {{ config.pdfWidth }}, {{ config.pdfHeight }})
})
Expand All @@ -46,7 +47,7 @@
hash: true,

// Learn about plugins: https://revealjs.com/plugins/
plugins: [RevealAwesoMD, RevealHighlight, RevealNotes],
plugins: [RevealAwesoMD, RevealHighlight, RevealNotes, RevealMermaid],
})
</script>
</body>
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/testFiles/markdown/test/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,14 @@ some content
# Section Slide for long TOC
# Section Slide for long TOC
# Section Slide for long TOC

## Mermaid JS

```mermaid
flowchart TD
A[Start] --> B{Is it?};
B -- Yes --> C[OK];
C --> D[Rethink];
D --> B;
B -- No ----> E[End];
```
11 changes: 11 additions & 0 deletions utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ function adjustFontSize() {
image.style.height = `${Math.floor(currentHeight * scaleFactor)}px`
})
}

// reduce chart size if font size gets smaller than minimum font size
const charts = wrapperElement.querySelectorAll('.mermaid')
if (fontSize <= fontSizeToStartReducingImage && charts.length > 0) {
charts.forEach((chart) => {
const chartElement = chart.querySelector('svg')
const bbox = chartElement.getBBox()
const chartHeight = bbox.height
chartElement.style.height = `${scaleFactor * chartHeight}px`
})
}
})

totalHeight = getTotalHeightOfChildren(content)
Expand Down

0 comments on commit 2a98377

Please sign in to comment.