Skip to content

Commit

Permalink
First working version
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerlong committed Apr 1, 2017
1 parent 26eb30f commit a0c2577
Show file tree
Hide file tree
Showing 4 changed files with 4,629 additions and 0 deletions.
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "markdown-it-chart",
"version": "0.1.0",
"description": "Chart.js plugin for markdown-it.",
"main": "src/index.bundle.js",
"scripts": {
"build": "node -r babel-register node_modules/.bin/webpack --progress --colors --watch",
"test": "node test/index.bundle.js",
"upgrade": "ncu -ua && yarn upgrade"
},
"repository": "[email protected]:tylingsoft/markdown-it-chart.git",
"author": "Tyler Long <[email protected]>",
"license": "MIT",
"devDependencies": {
"babel-core": "^6.24.0",
"babel-loader": "^6.4.1",
"babel-preset-env": "^1.2.2",
"markdown-it": "^8.3.1",
"npm-check-updates": "^2.10.4",
"standard": "^9.0.2",
"webpack": "^2.3.2",
"webpack-node-externals": "^1.5.4"
}
}
22 changes: 22 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const chartBlock = (code) => {
try {
let json = JSON.parse(code)
return `<canvas class="chartjs">${JSON.stringify(json)}</canvas>`
} catch (e) { // JSON.parse exception
return `<pre>${e}</pre>`
}
}

const ChartPlugin = (md) => {
const temp = md.renderer.rules.fence.bind(md.renderer.rules)
md.renderer.rules.fence = (tokens, idx, options, env, slf) => {
let token = tokens[idx]
let code = token.content.trim()
if (token.info === 'chart') {
return chartBlock(code)
}
return temp(tokens, idx, options, env, slf)
}
}

export default ChartPlugin
75 changes: 75 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import assert from 'assert'
import markdownIt from 'markdown-it'
import markdownItLatex from '../src/index'

const mdi = markdownIt()
mdi.use(markdownItLatex)

assert(mdi.render('# Hello world').trim() === '<h1>Hello world</h1>', '# Hello world')
assert(mdi.render('Hello world').trim() === '<p>Hello world</p>', 'Hello world')

console.log(mdi.render(`\`\`\`chart
{
"type": "pie",
"data": {
"labels": [
"Red",
"Blue",
"Yellow"
],
"datasets": [
{
"data": [
300,
50,
100
],
"backgroundColor": [
"#FF6384",
"#36A2EB",
"#FFCE56"
],
"hoverBackgroundColor": [
"#FF6384",
"#36A2EB",
"#FFCE56"
]
}
]
},
"options": {}
}
\`\`\``))

assert(mdi.render(`\`\`\`chart
{
"type": "pie",
"data": {
"labels": [
"Red",
"Blue",
"Yellow"
],
"datasets": [
{
"data": [
300,
50,
100
],
"backgroundColor": [
"#FF6384",
"#36A2EB",
"#FFCE56"
],
"hoverBackgroundColor": [
"#FF6384",
"#36A2EB",
"#FFCE56"
]
}
]
},
"options": {}
}
\`\`\``).indexOf('class="chartjs"') > -1)
Loading

0 comments on commit a0c2577

Please sign in to comment.