1
0
Fork 0
mirror of synced 2024-11-16 05:55:35 -05:00
ultimate-vim/sources_non_forked/markdown-preview.nvim/app/pages/chart.js

35 lines
916 B
JavaScript
Raw Normal View History

2022-05-19 08:12:11 -04:00
import Chart from 'chart.js'
function render () {
document.querySelectorAll('.chartjs').forEach(element => {
try {
// eslint-disable-next-line no-new
new Chart(element, JSON.parse(element.textContent))
} catch (e) {
element.outerHTML = `<pre>Chart.js complains: "${e}"</pre>`
}
})
}
const chartPlugin = (md) => {
const temp = md.renderer.rules.fence.bind(md.renderer.rules)
md.renderer.rules.fence = (tokens, idx, options, env, slf) => {
const token = tokens[idx]
if (token.info && token.info.trim() === 'chart') {
const code = token.content.trim()
try {
const json = JSON.parse(code)
return `<canvas class="chartjs">${JSON.stringify(json)}</canvas>`
} catch (e) { // JSON.parse exception
return `<pre>${e}</pre>`
}
}
return temp(tokens, idx, options, env, slf)
}
}
export default {
render,
chartPlugin
}