Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
Ir1d committed Oct 21, 2018
1 parent 9ceb4c7 commit 53071e2
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 0 deletions.
50 changes: 50 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
bin/linux_x64/clang-format

'use strict'

var visit = require('unist-util-visit')
var fs = require('fs')
spawn = require('child_process').spawn

function getNativeBinary() {
let nativeBinary;

if (os.platform() === 'win32') {
nativeBinary = path.join(`${__dirname}`, `../clang-format/bin/win32/clang-format.exe`);
} else {
nativeBinary = path.join(`${__dirname}`, `../clang-format/bin/${os.platform()}_${os.arch()}/clang-format`);
}

if (!fs.existsSync(nativeBinary)) {
const message = 'This module doesn\'t bundle the clang-format executable for your platform. ' +
`(${os.platform()}_${os.arch()})\n` +
'Consider installing it with your native package manager instead.\n';
throw new Error(message);
}
return nativeBinary;
}


function visitor(node) {
let nativeBinary = getNativeBinary()
if (node.type == 'code' || node.type == 'inlineCode') {
if (node.lang.toLowerCase() == 'c++' || node.lang.toLowerCase() == 'cpp') {
var child = spawnSync(nativeBinary);

child.stdin.setEncoding('utf-8');
child.stdout.pipe(process.stdout);

child.stdin.write(node.value);

child.stdin.end();

node.value = format(child.stdout)
}
}
}

module.exports = function attacher() {
return function transformer(tree, file) {
visit(tree, visitor)
}
}
134 changes: 134 additions & 0 deletions package-lock.json

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

27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "remark-clang-format",
"version": "0.0.1",
"description": "run clang-format for code in markdown",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Ir1d/remark-clang-format.git"
},
"keywords": [
"clang-format",
"remark"
],
"author": "Ir1d",
"license": "ISC",
"bugs": {
"url": "https://github.com/Ir1d/remark-clang-format/issues"
},
"homepage": "https://github.com/Ir1d/remark-clang-format#readme",
"dependencies": {
"clang-format": "^1.2.4",
"unist-util-visit": "^1.4.0"
}
}

0 comments on commit 53071e2

Please sign in to comment.