markdownstream
Advanced tools
Comparing version
# Changelog | ||
## 0.0.2 | ||
* Added heading detection | ||
* New synchronous API for when a stream is overkill, and you just want an array of tokens | ||
## 0.0.1 | ||
@@ -4,0 +9,0 @@ |
@@ -33,3 +33,3 @@ // | ||
// | ||
function CodeChunk() { | ||
function CodeBlock() { | ||
// super | ||
@@ -40,5 +40,5 @@ Chunk.apply(this, arguments) | ||
CodeChunk.prototype = new Chunk | ||
CodeBlock.prototype = new Chunk | ||
CodeChunk.prototype.render = function() { | ||
CodeBlock.prototype.render = function() { | ||
if (this.fenced || this.tags) { | ||
@@ -54,2 +54,17 @@ return "```" + (this.tags || '') + "\n" + this.content + "```\n" | ||
module.exports = { plain: Chunk, code: CodeChunk } | ||
// | ||
// Heading Chunk | ||
// | ||
function Heading() { | ||
// super | ||
Chunk.apply(this, arguments) | ||
this.type = 'heading' | ||
} | ||
Heading.prototype = new Chunk | ||
Heading.prototype.render = function() { | ||
return Array(this.level + 1).join("#") + " " + this.content | ||
} | ||
module.exports = { plain: Chunk, code_block: CodeBlock, heading: Heading } |
@@ -36,2 +36,13 @@ var stream = require('stream') | ||
MarkdownStream.sync = function(doc) { | ||
var m = new MarkdownStream | ||
, chunks = [] | ||
m.on('data', function(c) { chunks.push(c) }) | ||
m.write(doc) | ||
return chunks | ||
} | ||
module.exports = MarkdownStream |
@@ -17,3 +17,3 @@ var chunk = require('./chunk') | ||
ctx.data += data | ||
cb(new chunk.code(ctx.data, { fenced: true, content: ctx.fcode.content, tags: ctx.fcode.tags })) | ||
cb(new chunk.code_block(ctx.data, { fenced: true, content: ctx.fcode.content, tags: ctx.fcode.tags })) | ||
@@ -64,3 +64,3 @@ ctx.fcode = null | ||
cb(new chunk.code(ctx.data, { content: ctx.code.content })) | ||
cb(new chunk.code_block(ctx.data, { content: ctx.code.content })) | ||
if (ctx.code.blanks) { | ||
@@ -97,2 +97,10 @@ | ||
buffer = buffer.replace(lex.heading, function(data, indent, content) { | ||
found = true | ||
cb(new chunk.heading(data, { content: content, level: indent.length })) | ||
return '' | ||
}) | ||
if (found) continue | ||
buffer = buffer.replace(lex.other, function(data) { | ||
@@ -112,2 +120,3 @@ | ||
lex.codeblock = /^ ([^\n]*\n)/ | ||
lex.heading = /^(\#{1,6})[ \t]+(.*?)\s*\#*\n/ | ||
lex.other = /^.*\n/ | ||
@@ -114,0 +123,0 @@ lex.blankline = /^[ \t]*\n/ |
{ | ||
"name": "markdownstream", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Streaming markdown parser that allows round-tripping and patching", | ||
@@ -5,0 +5,0 @@ "main": "lib/", |
@@ -33,2 +33,3 @@ var test = require('tap').test | ||
, fcode = [] | ||
, headings = [] | ||
@@ -39,10 +40,17 @@ input.pipe(parser) | ||
buffer += chunk | ||
if (chunk.type == 'code_block' && !chunk.fenced) { | ||
code.push(chunk.toString()) | ||
if (chunk.type == 'code_block') { | ||
if (chunk.fenced) | ||
fcode.push(chunk.toString()) | ||
else | ||
code.push(chunk.toString()) | ||
codeparsed.push(chunk.content) | ||
} | ||
if (chunk.type == 'code_block' && chunk.fenced) { | ||
fcode.push(chunk.toString()) | ||
codeparsed.push(chunk.content) | ||
if (chunk.type == 'heading') { | ||
headings.push(chunk.content) | ||
} | ||
output.write(chunk) | ||
@@ -59,2 +67,3 @@ }) | ||
t.deepEqual(codeparsed, ["IC1\nIC1\n\nIC1\n", "\nIC2\n\n", "FC1\n\nFC1\n\n", "FC2\nFC2\n\n FC2\n"]) | ||
t.deepEqual(headings, ["H11", "H21", "H12"]) | ||
t.end() | ||
@@ -76,7 +85,5 @@ }) | ||
chunk.tags = 'foo' | ||
output += chunk.render() | ||
} else { | ||
output += chunk.toString() | ||
chunk.refresh() | ||
} | ||
output += chunk.toString() | ||
}) | ||
@@ -93,1 +100,9 @@ | ||
}) | ||
test("can operate synchronously", function(t) { | ||
var output = MarkdownStream.sync(doc) | ||
t.equal(output.join(''), doc) | ||
t.end() | ||
}) |
@@ -1,6 +0,6 @@ | ||
# H1 | ||
# H11 | ||
T1 | ||
## H2 | ||
## H21 | ||
@@ -34,3 +34,3 @@ IC1 | ||
# H1 | ||
# H12 | ||
@@ -37,0 +37,0 @@ > BQ |
11232
11.1%279
13.88%