Skip to content

Instantly share code, notes, and snippets.

@matsu-chara
Last active June 8, 2016 01:17
Show Gist options
  • Select an option

  • Save matsu-chara/7197dbd9d8e8f18674d5501317465b1c to your computer and use it in GitHub Desktop.

Select an option

Save matsu-chara/7197dbd9d8e8f18674d5501317465b1c to your computer and use it in GitHub Desktop.
markdown内のjsonコードブロックを拾ってjsonlintかけてくれる君
~sand/markdown-json-lint $ node index.js sample.md
Parse error on line 1:
{ "b: 'a' }
--^
Expecting 'STRING', '}', got 'undefined'
Parse error on line 1:
{ "x: 2 }
--^
Expecting 'STRING', '}', got 'undefined'
~sand/markdown-json-lint $ node index.js sample2.md
~sand/markdown-json-lint $
var glob = require("glob");
var fs = require("fs");
var mdParser = require("markdown-to-ast");
var jsonParser = require("jsonlint").parser;
var filepattern = process.argv[2];
var filenames = glob.sync(filepattern)
var isError = false
filenames.forEach(function(filename) {
console.log(filename);
var markdown = fs.readFileSync(filename, "utf8");
var AST = mdParser.parse(markdown);
var jsons = findJsons(AST, []);
isError = checkJsons(jsons) || isError;
});
if(isError) {
process.exit(1);
}
function findJsons(node, acc) {
if(node.type == "CodeBlock" && node.lang == "json") {
acc.push(node.value);
}
if(node.children) {
node.children.forEach(function(e) {
findJsons(e, acc);
});
}
return acc;
}
function checkJsons(jsons) {
var isError=false;
jsons.forEach(function(j) {
try {
jsonParser.parse(j);
} catch (e) {
isError=true;
console.log(e.message);
}
});
return isError;
}
{
"name": "markdown-json-lint",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"jsonlint": "^1.6.2",
"markdown-to-ast": "^3.2.3",
"node-glob": "^1.2.0"
}
}

this is a chapter

here text

aaa

{ "a": 1 }
<?php
echo 'ok';
{ "b: 'a' }
{ "b": 2 }
{ "x: 2 }

yeah

this is a chapter

here text

aaa

{ "a": 1 }
<?php
echo 'ok';
{ "b": "a" }
{ "b": 2 }
{ "x": 3.2 }

yeah

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment