Skip to content

Commit

Permalink
[tests] add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
geekplux committed Jul 12, 2017
1 parent cf67c2d commit 94c2c02
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 97 deletions.
4 changes: 4 additions & 0 deletions .babelrc
@@ -0,0 +1,4 @@
{
"comments": false,
"presets": ["es2015"]
}
14 changes: 14 additions & 0 deletions .travis.yml
@@ -0,0 +1,14 @@
language: node_js
node_js:
- "8"
- "7"
- "6"
- "5"
- "4"
cache:
directories:
- "node_modules"
yarn: true
sudo: false
after_success:
- npm run test:cover:travis
24 changes: 24 additions & 0 deletions __tests__/index.js

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

4 changes: 2 additions & 2 deletions dist/index.js
@@ -1,7 +1,7 @@
'use strict';

const mdFence = require('markdown-it-fence');
const render = require('./render');
var mdFence = require('markdown-it-fence');
var render = require('./render');

module.exports = function (md, options) {
return mdFence(md, 'vis', {
Expand Down
33 changes: 15 additions & 18 deletions dist/render.js
@@ -1,42 +1,39 @@
const yaml = require('js-yaml');
const markvisBar = require('markvis-bar');
const markvisLine = require('markvis-line');
const markvisPie = require('markvis-pie');
'use strict';

var yaml = require('js-yaml');
var markvisBar = require('markvis-bar');
var markvisLine = require('markvis-line');
var markvisPie = require('markvis-pie');

function render(options) {
return function (tokens, idx, _options, env, self) {
const token = tokens[idx];
// if (token.hidden) return '';
return function (tokens, idx, _options, env) {
var token = tokens[idx];

let doc;
var doc = void 0;

// load content
try {
doc = yaml.load(token.content);
} catch (err) {
throw err;
}

const defaultChart = {
var defaultChart = {
bar: markvisBar,
line: markvisLine,
pie: markvisPie
};

let chart = defaultChart;
var chart = defaultChart;

// insert custom chart if any
if (options.hasOwnProperty('chart')) {
if (options && Object.prototype.hasOwnProperty.call(options, 'chart')) {
chart = Object.assign({}, defaultChart, options.chart);
}

const renderer = chart[doc.layout];
var renderer = chart[doc.layout];

// all options merged
const opts = Object.assign({}, options, env, doc);
var opts = Object.assign({}, options, env, doc);

// render the content
const result = renderer ? renderer(opts) : token.content;
var result = renderer ? renderer(opts) : token.content;

return result;
};
Expand Down
79 changes: 2 additions & 77 deletions examples/basic.js
Expand Up @@ -2,84 +2,9 @@ const fs = require('fs')
const d3node = require('d3-node')
const md = require('markdown-it')()
const vis = require('../src/index.js')
const content = require('./content')

const testStr = md.use(vis).render(`
# Hello World
I'm **testing**!
## Test
console.log('inline code');
\`\`\`js
console.log('language javascript')
\`\`\`
\`\`\`vis
layout: bar
data: [
{ key: 0, value: 5 },
{ key: 1, value: 4 },
{ key: 2, value: 7 },
{ key: 3, value: 2 },
{ key: 4, value: 4 },
{ key: 5, value: 8 },
{ key: 6, value: 3 },
{ key: 7, value: 6 }
]
\`\`\`
\`\`\`vis
layout: line
data: [
{ key: 0, value: 5 },
{ key: 1, value: 4 },
{ key: 2, value: 7 },
{ key: 3, value: 2 },
{ key: 4, value: 4 },
{ key: 5, value: 8 },
{ key: 6, value: 3 },
{ key: 7, value: 6 }
]
\`\`\`
\`\`\`vis
layout: pie
data: [
{ key: 0, value: 5 },
{ key: 1, value: 4 },
{ key: 2, value: 7 },
{ key: 3, value: 2 },
{ key: 4, value: 4 },
{ key: 5, value: 8 },
{ key: 6, value: 3 },
{ key: 7, value: 6 }
]
\`\`\`
\`\`\`vis
layout: no_layout
data: [
{ key: 0, value: 5 },
{ key: 1, value: 4 },
{ key: 2, value: 7 },
{ key: 3, value: 2 },
{ key: 4, value: 4 },
{ key: 5, value: 8 },
{ key: 6, value: 3 },
{ key: 7, value: 6 }
]
\`\`\`
`, {
d3node
})

console.log(testStr)
const testStr = md.use(vis).render(content(), { d3node })

fs.writeFile('./examples/basic.html', testStr, err => {
if (err) throw err
Expand Down
73 changes: 73 additions & 0 deletions examples/content.js
@@ -0,0 +1,73 @@
module.exports = () => `
# Hello World
I'm **testing**!
## Test
console.log('inline code');
\`\`\`js
console.log('language javascript')
\`\`\`
\`\`\`vis
layout: bar
data: [
{ key: 0, value: 5 },
{ key: 1, value: 4 },
{ key: 2, value: 7 },
{ key: 3, value: 2 },
{ key: 4, value: 4 },
{ key: 5, value: 8 },
{ key: 6, value: 3 },
{ key: 7, value: 6 }
]
\`\`\`
\`\`\`vis
layout: line
data: [
{ key: 0, value: 5 },
{ key: 1, value: 4 },
{ key: 2, value: 7 },
{ key: 3, value: 2 },
{ key: 4, value: 4 },
{ key: 5, value: 8 },
{ key: 6, value: 3 },
{ key: 7, value: 6 }
]
\`\`\`
\`\`\`vis
layout: pie
data: [
{ key: 0, value: 5 },
{ key: 1, value: 4 },
{ key: 2, value: 7 },
{ key: 3, value: 2 },
{ key: 4, value: 4 },
{ key: 5, value: 8 },
{ key: 6, value: 3 },
{ key: 7, value: 6 }
]
\`\`\`
\`\`\`vis
layout: no_layout
data: [
{ key: 0, value: 5 },
{ key: 1, value: 4 },
{ key: 2, value: 7 },
{ key: 3, value: 2 },
{ key: 4, value: 4 },
{ key: 5, value: 8 },
{ key: 6, value: 3 },
{ key: 7, value: 6 }
]
\`\`\`
`

0 comments on commit 94c2c02

Please sign in to comment.