tidy-markdown
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -8,11 +8,2 @@ #!/usr/bin/env node | ||
tidyMarkdown = require('../lib'); | ||
process.stdin.setEncoding('utf8'); | ||
process.stdin.on('readable', function() { | ||
var buffer = ''; | ||
while (null !== (chunk = process.stdin.read())) { | ||
buffer += chunk | ||
} | ||
process.stdout.write(tidyMarkdown(buffer)); | ||
}); | ||
require('../lib/cli'); |
137
lib/index.js
// Generated by CoffeeScript 1.8.0 | ||
(function() { | ||
var CODE_REGEX, Entities, IMG_REGEX, LINK_REGEX, delimitCode, fixHeaders, fm, htmlEntities, indent, longestStringInArray, marked, nestContainingTokens, nestingEndTokens, nestingStartTokens, pad, preprocessAST, prettyInlineMarkdown, yaml, | ||
var CODE_REGEX, Entities, IMG_REGEX, LINK_REGEX, delimitCode, fixHeaders, fm, formatTable, htmlEntities, indent, longestStringInArray, marked, nestContainingTokens, nestingEndTokens, nestingStartTokens, pad, preprocessAST, prettyInlineMarkdown, yaml, | ||
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | ||
@@ -203,14 +203,30 @@ | ||
fixHeaders = function(ast) { | ||
var e, gap, i, lastHeaderDepth, parentDepth; | ||
fixHeaders = function(ast, ensureFirstHeaderIsH1) { | ||
var e, gap, i, lastHeaderDepth, parentDepth, rootDepth, _ref; | ||
i = 0; | ||
lastHeaderDepth = 0; | ||
if (!ensureFirstHeaderIsH1) { | ||
e = 0; | ||
while (e < ast.length) { | ||
if (ast[e].type !== 'heading') { | ||
e++; | ||
} else { | ||
lastHeaderDepth = ast[e].depth - 1; | ||
break; | ||
} | ||
} | ||
} | ||
rootDepth = lastHeaderDepth + 1; | ||
while (i < ast.length) { | ||
if (ast[i].type !== 'heading') { | ||
} else if (ast[i].depth <= lastHeaderDepth + 1) { | ||
} else if ((rootDepth <= (_ref = ast[i].depth) && _ref <= lastHeaderDepth + 1)) { | ||
lastHeaderDepth = ast[i].depth; | ||
} else { | ||
e = i; | ||
gap = ast[i].depth - (lastHeaderDepth + 1); | ||
if (ast[i].depth <= rootDepth) { | ||
gap = ast[i].depth - rootDepth; | ||
} else { | ||
gap = ast[i].depth - (lastHeaderDepth + 1); | ||
} | ||
parentDepth = ast[i].depth; | ||
@@ -234,5 +250,60 @@ while (e < ast.length) { | ||
module.exports = function(dirtyMarkdown) { | ||
var alignment, ast, col, colWidth, content, i, id, j, line, link, links, openLooseItem, optionalTitle, out, previousToken, row, token, _i, _j, _k, _l, _len, _len1, _len2, _len3, _m, _n, _o, _ref, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7; | ||
formatTable = function(token) { | ||
var alignment, col, colWidth, i, j, out, row, _i, _j, _k, _l, _len, _len1, _m, _ref, _ref1, _ref2, _ref3, _ref4; | ||
out = []; | ||
for (i = _i = 0, _ref = token.header.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { | ||
col = [token.header[i]]; | ||
for (j = _j = 0, _ref1 = token.cells.length; 0 <= _ref1 ? _j < _ref1 : _j > _ref1; j = 0 <= _ref1 ? ++_j : --_j) { | ||
token.cells[j][i] = (token.cells[j][i] != null ? token.cells[j][i].trim() : ''); | ||
col.push(token.cells[j][i]); | ||
} | ||
colWidth = longestStringInArray(col); | ||
token.header[i] = pad(token.header[i], colWidth); | ||
alignment = token.align[i]; | ||
token.align[i] = ((function() { | ||
switch (alignment) { | ||
case null: | ||
return pad('', colWidth, '-'); | ||
case 'left': | ||
return ':' + pad('', colWidth - 1, '-'); | ||
case 'center': | ||
return ':' + pad('', colWidth - 2, '-') + ':'; | ||
case 'right': | ||
return pad('', colWidth - 1, '-') + ':'; | ||
} | ||
})()); | ||
for (j = _k = 0, _ref2 = token.cells.length; 0 <= _ref2 ? _k < _ref2 : _k > _ref2; j = 0 <= _ref2 ? ++_k : --_k) { | ||
token.cells[j][i] = (alignment === 'right' ? pad(colWidth, token.cells[j][i]) : pad(token.cells[j][i], colWidth)); | ||
} | ||
} | ||
if (token.header.length > 1) { | ||
out.push(token.header.join(' | ').trimRight()); | ||
out.push(token.align.join(' | ')); | ||
_ref3 = token.cells; | ||
for (_l = 0, _len = _ref3.length; _l < _len; _l++) { | ||
row = _ref3[_l]; | ||
out.push(row.join(' | ').trimRight()); | ||
} | ||
} else { | ||
out.push('| ' + token.header[0].trimRight()); | ||
out.push('| ' + token.align[0]); | ||
_ref4 = token.cells; | ||
for (_m = 0, _len1 = _ref4.length; _m < _len1; _m++) { | ||
row = _ref4[_m]; | ||
out.push('| ' + row[0].trimRight()); | ||
} | ||
} | ||
out.push(''); | ||
return out; | ||
}; | ||
module.exports = function(dirtyMarkdown, options) { | ||
var ast, content, id, line, link, links, openLooseItem, optionalTitle, out, previousToken, token, _i, _j, _len, _len1, _ref, _ref1, _ref2; | ||
if (options == null) { | ||
options = {}; | ||
} | ||
if (options.ensureFirstHeaderIsH1 == null) { | ||
options.ensureFirstHeaderIsH1 = true; | ||
} | ||
out = []; | ||
content = fm(dirtyMarkdown); | ||
@@ -262,3 +333,3 @@ if (Object.keys(content.attributes).length !== 0) { | ||
ast = preprocessAST(ast); | ||
ast = fixHeaders(ast); | ||
ast = fixHeaders(ast, options.ensureFirstHeaderIsH1); | ||
for (_i = 0, _len = ast.length; _i < _len; _i++) { | ||
@@ -303,45 +374,3 @@ token = ast[_i]; | ||
} | ||
for (i = _j = 0, _ref2 = token.header.length; 0 <= _ref2 ? _j < _ref2 : _j > _ref2; i = 0 <= _ref2 ? ++_j : --_j) { | ||
col = [token.header[i]]; | ||
for (j = _k = 0, _ref3 = token.cells.length; 0 <= _ref3 ? _k < _ref3 : _k > _ref3; j = 0 <= _ref3 ? ++_k : --_k) { | ||
token.cells[j][i] = token.cells[j][i].trim(); | ||
col.push(token.cells[j][i]); | ||
} | ||
colWidth = longestStringInArray(col); | ||
token.header[i] = pad(token.header[i], colWidth); | ||
alignment = token.align[i]; | ||
token.align[i] = ((function() { | ||
switch (alignment) { | ||
case null: | ||
return pad('', colWidth, '-'); | ||
case 'left': | ||
return ':' + pad('', colWidth - 1, '-'); | ||
case 'center': | ||
return ':' + pad('', colWidth - 2, '-') + ':'; | ||
case 'right': | ||
return pad('', colWidth - 1, '-') + ':'; | ||
} | ||
})()); | ||
for (j = _l = 0, _ref4 = token.cells.length; 0 <= _ref4 ? _l < _ref4 : _l > _ref4; j = 0 <= _ref4 ? ++_l : --_l) { | ||
token.cells[j][i] = (alignment === 'right' ? pad(colWidth, token.cells[j][i]) : pad(token.cells[j][i], colWidth)); | ||
} | ||
} | ||
if (token.header.length > 1) { | ||
out.push(token.header.join(' | ').trimRight()); | ||
out.push(token.align.join(' | ')); | ||
_ref5 = token.cells; | ||
for (_m = 0, _len1 = _ref5.length; _m < _len1; _m++) { | ||
row = _ref5[_m]; | ||
out.push(row.join(' | ').trimRight()); | ||
} | ||
} else { | ||
out.push('| ' + token.header[0].trimRight()); | ||
out.push('| ' + token.align[0]); | ||
_ref6 = token.cells; | ||
for (_n = 0, _len2 = _ref6.length; _n < _len2; _n++) { | ||
row = _ref6[_n]; | ||
out.push('| ' + row[0].trimRight()); | ||
} | ||
} | ||
out.push(''); | ||
out.push.apply(out, formatTable(token)); | ||
break; | ||
@@ -355,5 +384,5 @@ case 'hr': | ||
case 'html': | ||
_ref7 = token.text.split('\n'); | ||
for (_o = 0, _len3 = _ref7.length; _o < _len3; _o++) { | ||
line = _ref7[_o]; | ||
_ref2 = token.text.split('\n'); | ||
for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) { | ||
line = _ref2[_j]; | ||
out.push(line); | ||
@@ -360,0 +389,0 @@ } |
{ | ||
"name": "tidy-markdown", | ||
"description": "fix ugly markdown.", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"author": "Sean Lang", | ||
@@ -10,2 +10,3 @@ "bin": { | ||
"dependencies": { | ||
"argparse": "^1.0.1", | ||
"front-matter": "^1.0.0", | ||
@@ -12,0 +13,0 @@ "html-entities": "^1.0.10", |
@@ -7,3 +7,3 @@ # Tidy Markdown | ||
## Install | ||
Tidy Markdown is an [npm](npmjs.org) package, so it can be installed like this: | ||
Tidy Markdown is an [npm](http://npmjs.org/package/tidy-markdown) package, so it can be installed like this: | ||
@@ -15,3 +15,3 @@ ```bash | ||
## CLI | ||
Tidy Markdown includes a simple CLI. It (currently) takes no arguments and operates entirely over STDIN/STDOUT. For example: | ||
Tidy Markdown includes a simple CLI. It operates entirely over STDIN/STDOUT. For example: | ||
@@ -40,2 +40,19 @@ ```bash | ||
The `--help` arg will make it show a usage page: | ||
``` | ||
usage: tidy-markdown [-h] [-v] [--no-ensure-first-header-is-h1] | ||
fix ugly markdown. | ||
Optional arguments: | ||
-h, --help Show this help message and exit. | ||
-v, --version Show program's version number and exit. | ||
--no-ensure-first-header-is-h1 | ||
Disable fixing the first header when it isn't an H1. | ||
This is useful if the markdown you're processing | ||
isn't a full document, but rather a piece of a larger | ||
document. | ||
``` | ||
## API | ||
@@ -77,2 +94,2 @@ Tidy Markdown only exports one function, that takes one argument (the string of ugly markdown). Here's an example of how it can be used: | ||
- decode Unicode characters that have markdown equivalents (like a horizontal ellipsis becomes "..." and an em-dash becomes "--"). | ||
- move long or repeated links into the reference links section. | ||
- format YAML front-matter and Markdown tables |
53554
8
411
92
7
+ Addedargparse@^1.0.1