Comparing version 0.0.1 to 0.0.2
@@ -102,18 +102,22 @@ 'use strict'; | ||
var chars = chunk.toString().split(''); | ||
try { | ||
var chars = chunk.toString().split(''); | ||
chars.forEach(function (char, i) { | ||
_this2.currentLine += char; | ||
chars.forEach(function (char, i) { | ||
_this2.currentLine += char; | ||
/* If the current line is not a full line, there is nothing to | ||
process. */ | ||
if (!/\n/.test(_this2.currentLine)) { | ||
return; | ||
} | ||
/* If the current line is not a full line, there is nothing to | ||
process. */ | ||
if (!/\n/.test(_this2.currentLine)) { | ||
return; | ||
} | ||
_this2.processCurrentLine(); | ||
}); | ||
_this2.processCurrentLine(); | ||
}); | ||
/* Finished processing this chunk */ | ||
return callback(); | ||
/* Finished processing this chunk */ | ||
return callback(); | ||
} catch (error) { | ||
return callback(error); | ||
} | ||
} | ||
@@ -123,11 +127,15 @@ }, { | ||
value: function _flush(callback) { | ||
/* If our multiline input went till the EOF */ | ||
if (this.lines.length) { | ||
this.push(this.replaceContent(this.lines.join(''))); | ||
} else { | ||
/* Output any lingering content at the EOF */ | ||
this.push(this.currentLine); | ||
try { | ||
/* If our multiline input went till the EOF */ | ||
if (this.lines.length) { | ||
this.push(this.replaceContent(this.lines.join(''))); | ||
} else { | ||
/* Output any lingering content at the EOF */ | ||
this.push(this.currentLine); | ||
} | ||
return callback(); | ||
} catch (error) { | ||
return callback(error); | ||
} | ||
return callback(); | ||
} | ||
@@ -196,71 +204,76 @@ }]); | ||
var chars = chunk.toString().split(''); | ||
try { | ||
chars.forEach(function (char, i) { | ||
/* Add character to our cache */ | ||
_this4.cache.push(char); | ||
var chars = chunk.toString().split(''); | ||
/* If our cache is too large, remove the oldest character */ | ||
if (_this4.cache.length > _this4.cacheLength) { | ||
_this4.cache.shift(); | ||
} | ||
chars.forEach(function (char, i) { | ||
/* Add character to our cache */ | ||
_this4.cache.push(char); | ||
/* Form a new word from the cache */ | ||
var word = _this4.cache.join(''); | ||
/* If our cache is too large, remove the oldest character */ | ||
if (_this4.cache.length > _this4.cacheLength) { | ||
_this4.cache.shift(); | ||
} | ||
/* The reason why we negate the value here it to only get the | ||
values at the end of the cache should the start & end words provided | ||
be of different lengths. */ | ||
var foundStartWord = word.slice(-1 * _this4.startLength); | ||
var foundEndWord = word.slice(-1 * _this4.endLength); | ||
/* Form a new word from the cache */ | ||
var word = _this4.cache.join(''); | ||
var startMatch = _this4.startRegex.test(foundStartWord); | ||
var endMatch = _this4.endRegex.test(foundEndWord); | ||
/* The reason why we negate the value here it to only get the | ||
values at the end of the cache should the start & end words provided | ||
be of different lengths. */ | ||
var foundStartWord = word.slice(-1 * _this4.startLength); | ||
var foundEndWord = word.slice(-1 * _this4.endLength); | ||
/* If we have not started, ignore anything that isn't the | ||
specified starting word and pass along this character | ||
to another stream. */ | ||
if (!_this4.started && !startMatch) { | ||
return _this4.beforeSpan += char; | ||
} | ||
var startMatch = _this4.startRegex.test(foundStartWord); | ||
var endMatch = _this4.endRegex.test(foundEndWord); | ||
/* If we have not started and find our starting word, then | ||
we start the current span, record the start word for | ||
the current span, clear the cache (to avoid false | ||
positives on end words), push the chunk contents before | ||
this span started, and carry on. */ | ||
if (!_this4.started && startMatch) { | ||
_this4.push(_this4.beforeSpan.slice(0, -1 * (_this4.startLength - 1))); | ||
_this4.beforeSpan = ''; | ||
/* If we have not started, ignore anything that isn't the | ||
specified starting word and pass along this character | ||
to another stream. */ | ||
if (!_this4.started && !startMatch) { | ||
return _this4.beforeSpan += char; | ||
} | ||
_this4.started = true; | ||
_this4.currentSpan = foundStartWord; | ||
_this4.cache = []; | ||
/* If we have not started and find our starting word, then | ||
we start the current span, record the start word for | ||
the current span, clear the cache (to avoid false | ||
positives on end words), push the chunk contents before | ||
this span started, and carry on. */ | ||
if (!_this4.started && startMatch) { | ||
_this4.push(_this4.beforeSpan.slice(0, -1 * (_this4.startLength - 1))); | ||
_this4.beforeSpan = ''; | ||
return; | ||
} | ||
_this4.started = true; | ||
_this4.currentSpan = foundStartWord; | ||
_this4.cache = []; | ||
/* If we have started and find our stopping word, then we | ||
stop the current span, record this current character | ||
for the current span (to complete this end word), call | ||
the `replaceSpan` function, clear the current span, and | ||
finally report the results to whatever stream uses this | ||
one. */ | ||
if (_this4.started && endMatch) { | ||
_this4.started = false; | ||
_this4.currentSpan += char; | ||
var replacedSpan = _this4.replaceSpan(_this4.currentSpan); | ||
_this4.currentSpan = ''; | ||
return _this4.push(replacedSpan); | ||
} | ||
return; | ||
} | ||
/* If we have not found a start or end word, but we are | ||
within a current span, record all characters inbetween */ | ||
if (_this4.started) { | ||
_this4.currentSpan += char; | ||
} | ||
}); | ||
/* If we have started and find our stopping word, then we | ||
stop the current span, record this current character | ||
for the current span (to complete this end word), call | ||
the `replaceSpan` function, clear the current span, and | ||
finally report the results to whatever stream uses this | ||
one. */ | ||
if (_this4.started && endMatch) { | ||
_this4.started = false; | ||
_this4.currentSpan += char; | ||
var replacedSpan = _this4.replaceSpan(_this4.currentSpan); | ||
_this4.currentSpan = ''; | ||
return _this4.push(replacedSpan); | ||
} | ||
/* Finished processing this chunk */ | ||
return callback(); | ||
/* If we have not found a start or end word, but we are | ||
within a current span, record all characters inbetween */ | ||
if (_this4.started) { | ||
_this4.currentSpan += char; | ||
} | ||
}); | ||
/* Finished processing this chunk */ | ||
return callback(); | ||
} catch (error) { | ||
return callback(error); | ||
} | ||
} | ||
@@ -311,37 +324,42 @@ }, { | ||
/* Get a list of references that exist in this document, and | ||
pull out their id and associated link. */ | ||
var references = this.document.split('\n').filter(function (line) { | ||
return (/\[\w+\]:/.test(line) | ||
); | ||
}).map(function (refString) { | ||
var match = refString.match(/\[([\w ]+)\]: ?(.*)$/); | ||
var id = match[1]; | ||
var link = match[2].match(/"(.*)"/)[1]; | ||
return { id: id, link: link }; | ||
}); | ||
try { | ||
/* Go through all the references, and find their use | ||
throughout the document. */ | ||
references.forEach(function (ref) { | ||
var reString = '\\[([\\w ]+)\\]\\[(' + ref.id + ')\\]'; | ||
var re = new RegExp(reString); | ||
/* Get a list of references that exist in this document, and | ||
pull out their id and associated link. */ | ||
var references = this.document.split('\n').filter(function (line) { | ||
return (/\[[\w- ]+\]: ?(.{5,})$/.test(line) | ||
); | ||
}).map(function (refString) { | ||
var match = refString.match(/\[([\w- ]+)\]: ?(.{5,})$/); | ||
var id = match[1]; | ||
var link = match[2].match(/"(.*)"/)[1]; | ||
return { id: id, link: link }; | ||
}); | ||
/* While there is a useage of a reference in the document, | ||
replace it with the proper syntax. */ | ||
while (re.test(_this6.document)) { | ||
var match = _this6.document.match(re); | ||
var text = match[1]; | ||
/* Go through all the references, and find their use | ||
throughout the document. */ | ||
references.forEach(function (ref) { | ||
var reString = '\\[([^\\]]+)\\]\\[(' + ref.id + ')\\]'; | ||
var re = new RegExp(reString); | ||
var replace = '<span class="pm-syntax">[</span>' + ('<a href="' + ref.link + '">' + text + '</a>') + '<span class="pm-syntax">]</span>' + '<span class="pm-syntax">[</span>' + ('<a href="#' + ref.id + '">' + ref.id + '</a>') + '<span class="pm-syntax">]</span>'; | ||
/* While there is a useage of a reference in the document, | ||
replace it with the proper syntax. */ | ||
while (re.test(_this6.document)) { | ||
var match = _this6.document.match(re); | ||
var text = match[1]; | ||
_this6.document = _this6.document.replace(re, replace); | ||
} | ||
var replace = '<span class="pm-syntax">[</span>' + ('<a href="' + ref.link + '">' + text + '</a>') + '<span class="pm-syntax">]</span>' + '<span class="pm-ref">' + '<span class="pm-syntax">[</span>' + ('<a href="#' + ref.id + '">' + ref.id + '</a>') + '<span class="pm-syntax">]</span>' + '</span>'; | ||
/* Replace this reference with the proper syntax. */ | ||
_this6.document = _this6.document.replace('[' + ref.id + ']', '[<span id="' + ref.id + '">' + ref.id + '</span>]'); | ||
}); | ||
_this6.document = _this6.document.replace(re, replace); | ||
} | ||
this.push(this.document); | ||
callback(); | ||
/* Replace this reference with the proper syntax. */ | ||
_this6.document = _this6.document.replace('[' + ref.id + ']', '<span class="pm-ref">[<span id="' + ref.id + '">' + ref.id + '</span>]</span>'); | ||
}); | ||
this.push(this.document); | ||
return callback(); | ||
} catch (error) { | ||
return callback(error); | ||
} | ||
} | ||
@@ -389,90 +407,123 @@ }]); | ||
var strongTransform = new SpanTransform({}, /\*\S/, 2, /\S\*/, 2, function (span) { | ||
var value = span.slice(1, -1); | ||
return '<strong><span class="pm-syntax">*</span>' + value + '<span class="pm-syntax">*</span></strong>'; | ||
}); | ||
var strongTransform = function strongTransform() { | ||
return new SpanTransform({}, /\s\*\S/, 3, /\S\*\s/, 3, function (span) { | ||
var start = span.slice(0, 1); | ||
var end = span.slice(-1); | ||
var value = span.slice(2, -2); | ||
return start + '<strong><span class="pm-syntax">*</span>' + value + '<span class="pm-syntax">*</span></strong>' + end; | ||
}); | ||
}; | ||
var emphasisTransform = new SpanTransform({}, /_\S/, 2, /\S_/, 2, function (span) { | ||
var value = span.slice(1, -1); | ||
return '<em><span class="pm-syntax">_</span>' + value + '<span class="pm-syntax">_</span></em>'; | ||
}); | ||
var emphasisTransform = function emphasisTransform() { | ||
return new SpanTransform({}, /\s_\S/, 3, /\S_\s/, 3, function (span) { | ||
var start = span.slice(0, 1); | ||
var end = span.slice(-1); | ||
var value = span.slice(2, -2); | ||
return start + '<em><span class="pm-syntax">_</span>' + value + '<span class="pm-syntax">_</span></em>' + end; | ||
}); | ||
}; | ||
var strikeTransform = new SpanTransform({}, /~\S/, 2, /\S~/, 2, function (span) { | ||
var value = span.slice(1, -1); | ||
return '<strike><span class="pm-syntax">˜</span>' + value + '<span class="pm-syntax">˜</span></strike>'; | ||
}); | ||
var strikeTransform = function strikeTransform() { | ||
return new SpanTransform({}, /\s~\S/, 3, /\S~\s/, 3, function (span) { | ||
var start = span.slice(0, 1); | ||
var end = span.slice(-1); | ||
var value = span.slice(2, -2); | ||
return start + '<strike><span class="pm-syntax">˜</span>' + value + '<span class="pm-syntax">˜</span></strike>' + end; | ||
}); | ||
}; | ||
var inlineCodeTransform = new SpanTransform({}, /`\S/, 2, /\S`/, 2, function (span) { | ||
var value = span.slice(1, -1); | ||
return '<code><span class="pm-syntax">`</span>' + value + '<span class="pm-syntax">`</span></code>'; | ||
}); | ||
var inlineCodeTransform = function inlineCodeTransform() { | ||
return new SpanTransform({}, /\s`\S/, 3, /\S`\s/, 3, function (span) { | ||
var start = span.slice(0, 1); | ||
var end = span.slice(-1); | ||
var value = span.slice(2, -2); | ||
return start + '<code><span class="pm-syntax">`</span>' + value + '<span class="pm-syntax">`</span></code>' + end; | ||
}); | ||
}; | ||
var inlineLinkTransform = new SpanTransform({}, /http/, 4, /[A-Za-z0-9\-\.\_\~\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=][^A-Za-z0-9\-\.\_\~\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=]/, 2, function (span) { | ||
var link = span.slice(0, -1); | ||
var endWith = span.slice(-1); | ||
return '<a href="' + link + '">' + link + '</a>' + endWith; | ||
}); | ||
var inlineLinkTransform = function inlineLinkTransform() { | ||
return new SpanTransform({}, /http/, 4, /[A-Za-z0-9\-\.\_\~\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=][^A-Za-z0-9\-\.\_\~\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=]/, 2, function (span) { | ||
var link = span.slice(0, -1); | ||
var endWith = span.slice(-1); | ||
return '<a href="' + link + '">' + link + '</a>' + endWith; | ||
}); | ||
}; | ||
var h1Transform = new LineTransform({}, /^#[^#].*/, /.*/, function (content) { | ||
var mod = content.slice(1, -1); | ||
return '<h1><span class="pm-syntax">#</span>' + mod + '</h1>\n'; | ||
}); | ||
var h1Transform = function h1Transform() { | ||
return new LineTransform({}, /^#[^#].*/, /.*/, function (content) { | ||
var mod = content.slice(1, -1); | ||
return '<h1><span class="pm-syntax">#</span>' + mod + '</h1>\n'; | ||
}); | ||
}; | ||
var h2Transform = new LineTransform({}, /^##[^#].*/, /.*/, function (content) { | ||
var mod = content.slice(2, -1); | ||
return '<h2><span class="pm-syntax">##</span>' + mod + '</h2>\n'; | ||
}); | ||
var h2Transform = function h2Transform() { | ||
return new LineTransform({}, /^##[^#].*/, /.*/, function (content) { | ||
var mod = content.slice(2, -1); | ||
return '<h2><span class="pm-syntax">##</span>' + mod + '</h2>\n'; | ||
}); | ||
}; | ||
var h3Transform = new LineTransform({}, /^###[^#].*/, /.*/, function (content) { | ||
var mod = content.slice(3, -1); | ||
return '<h2><span class="pm-syntax">###</span>' + mod + '</h3>\n'; | ||
}); | ||
var h3Transform = function h3Transform() { | ||
return new LineTransform({}, /^###[^#].*/, /.*/, function (content) { | ||
var mod = content.slice(3, -1); | ||
return '<h2><span class="pm-syntax">###</span>' + mod + '</h3>\n'; | ||
}); | ||
}; | ||
var h4Transform = new LineTransform({}, /^####[^#].*/, /.*/, function (content) { | ||
var mod = content.slice(4, -1); | ||
return '<h2><span class="pm-syntax">####</span>' + mod + '</h4>\n'; | ||
}); | ||
var h4Transform = function h4Transform() { | ||
return new LineTransform({}, /^####[^#].*/, /.*/, function (content) { | ||
var mod = content.slice(4, -1); | ||
return '<h2><span class="pm-syntax">####</span>' + mod + '</h4>\n'; | ||
}); | ||
}; | ||
var h5Transform = new LineTransform({}, /^#####[^#].*/, /.*/, function (content) { | ||
var mod = content.slice(5, -1); | ||
return '<h5><span class="pm-syntax">#####</span>' + mod + '</h5>\n'; | ||
}); | ||
var h5Transform = function h5Transform() { | ||
return new LineTransform({}, /^#####[^#].*/, /.*/, function (content) { | ||
var mod = content.slice(5, -1); | ||
return '<h5><span class="pm-syntax">#####</span>' + mod + '</h5>\n'; | ||
}); | ||
}; | ||
var horizontalRuleTransform = new LineTransform({}, /^ *(-|_|\*) *\1 *\1 *(\n|$)/, /.*/, function (content) { | ||
var mod = content.slice(0, -1); | ||
return '<span class="pm-syntax pm-hr">' + mod + '</span>\n'; | ||
}); | ||
var horizontalRuleTransform = function horizontalRuleTransform() { | ||
return new LineTransform({}, /^ *(-|_|\*) *\1 *\1 *(\n|$)/, /.*/, function (content) { | ||
var mod = content.slice(0, -1); | ||
return '<span class="pm-syntax pm-hr">' + mod + '</span>\n'; | ||
}); | ||
}; | ||
var blockquoteTransform = new LineTransform({}, /^>.*/, /^[^>].*/, function (content) { | ||
var mod = content.slice(0, -1).split('\n').map(function (line) { | ||
return line.replace(/^>/, '<span class="pm-syntax">></span>'); | ||
}).join('\n'); | ||
return '<blockquote>' + mod + '</blockquote>\n'; | ||
}); | ||
var blockquoteTransform = function blockquoteTransform() { | ||
return new LineTransform({}, /^>.*/, /^[^>].*/, function (content) { | ||
var mod = content.slice(0, -1).split('\n').map(function (line) { | ||
return line.replace(/^>/, '<span class="pm-syntax">></span>'); | ||
}).join('\n'); | ||
return '<blockquote>' + mod + '</blockquote>\n'; | ||
}); | ||
}; | ||
var codeTransform = new LineTransform({}, /^```/, /^```/, function (content) { | ||
var codeTransform = function codeTransform() { | ||
return new LineTransform({}, /^```/, /^```/, function (content) { | ||
var lang = content.match(/```(\w*)\n/)[1]; | ||
var lang = content.match(/```(\w*)\n/)[1]; | ||
var noFence = content.replace(/```(\n)?/g, ''); | ||
var noFence = content.replace(/```(\n)?/g, ''); | ||
var lines = noFence.split('\n'); | ||
var lines = noFence.split('\n'); | ||
/* If a language was specified, remove it from the lines of code */ | ||
if (lang) { | ||
lines.shift(); | ||
} | ||
/* If a language was specified, remove it from the lines of code */ | ||
if (lang) { | ||
lines.shift(); | ||
} | ||
var code = lines.join('\n').replace(/\*/g, '*').replace(/_/g, '_').replace(/~/g, '˜').replace(/`/g, '`'); | ||
var code = lines.join('\n').replace(/\*/g, '*').replace(/_/g, '_').replace(/~/g, '˜').replace(/`/g, '`'); | ||
if (lang) { | ||
code = (0, _highlight.highlight)(lang, code, true).value; | ||
} | ||
if (lang) { | ||
code = (0, _highlight.highlight)(lang, code, true).value; | ||
} | ||
return '<span class="pm-code">' + '<span class="pm-syntax">```</span>' + ('<span class="pm-lang">' + lang + '</span>\n') + code + '<span class="pm-syntax">```</span>' + '</span>\n'; | ||
}); | ||
}; | ||
return '<span class="pm-code">' + '<span class="pm-syntax">```</span>' + ('<span class="pm-lang">' + lang + '</span>\n') + code + '<span class="pm-syntax">```</span>' + '</span>'; | ||
}); | ||
function getStream(inStream) { | ||
return inStream.pipe(codeTransform).pipe(strongTransform).pipe(emphasisTransform).pipe(strikeTransform).pipe(inlineCodeTransform).pipe(inlineLinkTransform).pipe(h1Transform).pipe(h2Transform).pipe(h3Transform).pipe(h4Transform).pipe(h5Transform).pipe(horizontalRuleTransform).pipe(blockquoteTransform).pipe(new ReferenceLinkTransform()).pipe(new PreTransform()); | ||
function getStream(inStream, onError) { | ||
return inStream.pipe(codeTransform()).on('error', onError).pipe(strongTransform()).on('error', onError).pipe(emphasisTransform()).on('error', onError).pipe(strikeTransform()).on('error', onError).pipe(inlineCodeTransform()).on('error', onError).pipe(inlineLinkTransform()).on('error', onError).pipe(h1Transform()).on('error', onError).pipe(h2Transform()).on('error', onError).pipe(h3Transform()).on('error', onError).pipe(h4Transform()).on('error', onError).pipe(h5Transform()).on('error', onError).pipe(horizontalRuleTransform()).on('error', onError).pipe(blockquoteTransform()).on('error', onError).pipe(new ReferenceLinkTransform()).on('error', onError).pipe(new PreTransform()).on('error', onError); | ||
} | ||
{ | ||
"name": "premark", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "pretty preformatted text", | ||
@@ -5,0 +5,0 @@ "main": "./lib/premark.js", |
@@ -73,31 +73,40 @@ process.stdin.setEncoding('utf8'); | ||
_transform(chunk, encoding, callback) { | ||
try { | ||
const chars = chunk.toString().split(''); | ||
const chars = chunk.toString().split(''); | ||
chars.forEach((char, i) => { | ||
this.currentLine += char; | ||
chars.forEach((char, i) => { | ||
this.currentLine += char; | ||
/* If the current line is not a full line, there is nothing to | ||
process. */ | ||
if (!(/\n/.test(this.currentLine))) { | ||
return; | ||
} | ||
/* If the current line is not a full line, there is nothing to | ||
process. */ | ||
if (!(/\n/.test(this.currentLine))) { | ||
return; | ||
} | ||
this.processCurrentLine(); | ||
}); | ||
this.processCurrentLine(); | ||
}); | ||
/* Finished processing this chunk */ | ||
return callback(); | ||
/* Finished processing this chunk */ | ||
return callback(); | ||
} catch (error) { | ||
return callback(error); | ||
} | ||
} | ||
_flush(callback) { | ||
/* If our multiline input went till the EOF */ | ||
if (this.lines.length) { | ||
this.push(this.replaceContent(this.lines.join(''))); | ||
} else { | ||
/* Output any lingering content at the EOF */ | ||
this.push(this.currentLine); | ||
try { | ||
/* If our multiline input went till the EOF */ | ||
if (this.lines.length) { | ||
this.push(this.replaceContent(this.lines.join(''))); | ||
} else { | ||
/* Output any lingering content at the EOF */ | ||
this.push(this.currentLine); | ||
} | ||
return callback(); | ||
} catch (error) { | ||
return callback(error); | ||
} | ||
return callback(); | ||
} | ||
@@ -151,72 +160,77 @@ } | ||
_transform(chunk, encoding, callback) { | ||
try { | ||
const chars = chunk.toString().split(''); | ||
const chars = chunk.toString().split(''); | ||
chars.forEach((char, i) => { | ||
/* Add character to our cache */ | ||
this.cache.push(char); | ||
chars.forEach((char, i) => { | ||
/* Add character to our cache */ | ||
this.cache.push(char); | ||
/* If our cache is too large, remove the oldest character */ | ||
if (this.cache.length > this.cacheLength) { | ||
this.cache.shift(); | ||
} | ||
/* If our cache is too large, remove the oldest character */ | ||
if (this.cache.length > this.cacheLength) { | ||
this.cache.shift(); | ||
} | ||
/* Form a new word from the cache */ | ||
const word = this.cache.join(''); | ||
/* Form a new word from the cache */ | ||
const word = this.cache.join(''); | ||
/* The reason why we negate the value here it to only get the | ||
values at the end of the cache should the start & end words provided | ||
be of different lengths. */ | ||
const foundStartWord = word.slice(-1 * this.startLength); | ||
const foundEndWord = word.slice(-1 * this.endLength); | ||
/* The reason why we negate the value here it to only get the | ||
values at the end of the cache should the start & end words provided | ||
be of different lengths. */ | ||
const foundStartWord = word.slice(-1 * this.startLength); | ||
const foundEndWord = word.slice(-1 * this.endLength); | ||
const startMatch = this.startRegex.test(foundStartWord); | ||
const endMatch = this.endRegex.test(foundEndWord); | ||
const startMatch = this.startRegex.test(foundStartWord); | ||
const endMatch = this.endRegex.test(foundEndWord); | ||
/* If we have not started, ignore anything that isn't the | ||
specified starting word and pass along this character | ||
to another stream. */ | ||
if (!this.started && !startMatch) { | ||
return this.beforeSpan += char; | ||
} | ||
/* If we have not started, ignore anything that isn't the | ||
specified starting word and pass along this character | ||
to another stream. */ | ||
if (!this.started && !startMatch) { | ||
return this.beforeSpan += char; | ||
} | ||
/* If we have not started and find our starting word, then | ||
we start the current span, record the start word for | ||
the current span, clear the cache (to avoid false | ||
positives on end words), push the chunk contents before | ||
this span started, and carry on. */ | ||
if (!this.started && startMatch) { | ||
this.push(this.beforeSpan.slice(0, -1 * (this.startLength - 1))); | ||
this.beforeSpan = ''; | ||
/* If we have not started and find our starting word, then | ||
we start the current span, record the start word for | ||
the current span, clear the cache (to avoid false | ||
positives on end words), push the chunk contents before | ||
this span started, and carry on. */ | ||
if (!this.started && startMatch) { | ||
this.push(this.beforeSpan.slice(0, -1 * (this.startLength - 1))); | ||
this.beforeSpan = ''; | ||
this.started = true; | ||
this.currentSpan = foundStartWord; | ||
this.cache = []; | ||
this.started = true; | ||
this.currentSpan = foundStartWord; | ||
this.cache = []; | ||
return; | ||
} | ||
return; | ||
} | ||
/* If we have started and find our stopping word, then we | ||
stop the current span, record this current character | ||
for the current span (to complete this end word), call | ||
the `replaceSpan` function, clear the current span, and | ||
finally report the results to whatever stream uses this | ||
one. */ | ||
if (this.started && endMatch) { | ||
this.started = false; | ||
this.currentSpan += char; | ||
const replacedSpan = this.replaceSpan(this.currentSpan); | ||
this.currentSpan = ''; | ||
return this.push(replacedSpan); | ||
} | ||
/* If we have started and find our stopping word, then we | ||
stop the current span, record this current character | ||
for the current span (to complete this end word), call | ||
the `replaceSpan` function, clear the current span, and | ||
finally report the results to whatever stream uses this | ||
one. */ | ||
if (this.started && endMatch) { | ||
this.started = false; | ||
this.currentSpan += char; | ||
const replacedSpan = this.replaceSpan(this.currentSpan); | ||
this.currentSpan = ''; | ||
return this.push(replacedSpan); | ||
} | ||
/* If we have not found a start or end word, but we are | ||
within a current span, record all characters inbetween */ | ||
if (this.started) { | ||
this.currentSpan += char; | ||
} | ||
}); | ||
/* If we have not found a start or end word, but we are | ||
within a current span, record all characters inbetween */ | ||
if (this.started) { | ||
this.currentSpan += char; | ||
} | ||
}); | ||
/* Finished processing this chunk */ | ||
return callback(); | ||
/* Finished processing this chunk */ | ||
return callback(); | ||
} catch (error) { | ||
return callback(error); | ||
} | ||
} | ||
@@ -251,43 +265,51 @@ | ||
_flush(callback) { | ||
/* Get a list of references that exist in this document, and | ||
pull out their id and associated link. */ | ||
const references = this.document.split('\n').filter((line) => { | ||
return /\[\w+\]:/.test(line); | ||
}).map((refString) => { | ||
const match = refString.match(/\[([\w ]+)\]: ?(.*)$/); | ||
const id = match[1]; | ||
const link = match[2].match(/"(.*)"/)[1]; | ||
return { id, link }; | ||
}); | ||
try { | ||
/* Go through all the references, and find their use | ||
throughout the document. */ | ||
references.forEach((ref) => { | ||
const reString = `\\[([\\w ]+)\\]\\[(${ref.id})\\]`; | ||
const re = new RegExp(reString); | ||
/* Get a list of references that exist in this document, and | ||
pull out their id and associated link. */ | ||
const references = this.document.split('\n').filter((line) => { | ||
return /\[[\w- ]+\]: ?(.{5,})$/.test(line); | ||
}).map((refString) => { | ||
const match = refString.match(/\[([\w- ]+)\]: ?(.{5,})$/); | ||
const id = match[1]; | ||
const link = match[2].match(/"(.*)"/)[1]; | ||
return { id, link }; | ||
}); | ||
/* While there is a useage of a reference in the document, | ||
replace it with the proper syntax. */ | ||
while (re.test(this.document)) { | ||
const match = this.document.match(re); | ||
const text = match[1]; | ||
/* Go through all the references, and find their use | ||
throughout the document. */ | ||
references.forEach((ref) => { | ||
const reString = `\\[([^\\]]+)\\]\\[(${ref.id})\\]`; | ||
const re = new RegExp(reString); | ||
const replace = '<span class="pm-syntax">[</span>' + | ||
`<a href="${ref.link}">${text}</a>` + | ||
'<span class="pm-syntax">]</span>' + | ||
'<span class="pm-syntax">[</span>' + | ||
`<a href="#${ref.id}">${ref.id}</a>` + | ||
'<span class="pm-syntax">]</span>'; | ||
/* While there is a useage of a reference in the document, | ||
replace it with the proper syntax. */ | ||
while (re.test(this.document)) { | ||
const match = this.document.match(re); | ||
const text = match[1]; | ||
this.document = this.document.replace(re, replace); | ||
} | ||
const replace = '<span class="pm-syntax">[</span>' + | ||
`<a href="${ref.link}">${text}</a>` + | ||
'<span class="pm-syntax">]</span>' + | ||
'<span class="pm-ref">' + | ||
'<span class="pm-syntax">[</span>' + | ||
`<a href="#${ref.id}">${ref.id}</a>` + | ||
'<span class="pm-syntax">]</span>' + | ||
'</span>'; | ||
/* Replace this reference with the proper syntax. */ | ||
this.document = this.document.replace( | ||
`[${ref.id}]`, | ||
`[<span id="${ref.id}">${ref.id}</span>]`); | ||
}); | ||
this.document = this.document.replace(re, replace); | ||
} | ||
this.push(this.document); | ||
callback(); | ||
/* Replace this reference with the proper syntax. */ | ||
this.document = this.document.replace( | ||
`[${ref.id}]`, | ||
`<span class="pm-ref">[<span id="${ref.id}">${ref.id}</span>]</span>`); | ||
}); | ||
this.push(this.document); | ||
return callback(); | ||
} catch (error) { | ||
return callback(error); | ||
} | ||
} | ||
@@ -319,31 +341,46 @@ } | ||
const strongTransform = new SpanTransform({}, /\*\S/, 2, /\S\*/, 2, (span) => { | ||
const value = span.slice(1, -1); | ||
return '<strong><span class="pm-syntax">*</span>' + | ||
const strongTransform = () => new SpanTransform({}, /\s\*\S/, 3, /\S\*\s/, 3, (span) => { | ||
const start = span.slice(0, 1); | ||
const end = span.slice(-1); | ||
const value = span.slice(2, -2); | ||
return start + | ||
'<strong><span class="pm-syntax">*</span>' + | ||
value + | ||
'<span class="pm-syntax">*</span></strong>'; | ||
'<span class="pm-syntax">*</span></strong>' + | ||
end; | ||
}); | ||
const emphasisTransform = new SpanTransform({}, /_\S/, 2, /\S_/, 2, (span) => { | ||
const value = span.slice(1, -1); | ||
return '<em><span class="pm-syntax">_</span>' + | ||
const emphasisTransform = () => new SpanTransform({}, /\s_\S/, 3, /\S_\s/, 3, (span) => { | ||
const start = span.slice(0, 1); | ||
const end = span.slice(-1); | ||
const value = span.slice(2, -2); | ||
return start + | ||
'<em><span class="pm-syntax">_</span>' + | ||
value + | ||
'<span class="pm-syntax">_</span></em>'; | ||
'<span class="pm-syntax">_</span></em>' + | ||
end; | ||
}); | ||
const strikeTransform = new SpanTransform({}, /~\S/, 2, /\S~/, 2, (span) => { | ||
const value = span.slice(1, -1); | ||
return '<strike><span class="pm-syntax">˜</span>' + | ||
const strikeTransform = () => new SpanTransform({}, /\s~\S/, 3, /\S~\s/, 3, (span) => { | ||
const start = span.slice(0, 1); | ||
const end = span.slice(-1); | ||
const value = span.slice(2, -2); | ||
return start + | ||
'<strike><span class="pm-syntax">˜</span>' + | ||
value + | ||
'<span class="pm-syntax">˜</span></strike>'; | ||
'<span class="pm-syntax">˜</span></strike>' + | ||
end; | ||
}); | ||
const inlineCodeTransform = new SpanTransform({}, /`\S/, 2, /\S`/, 2, (span) => { | ||
const value = span.slice(1, -1); | ||
return '<code><span class="pm-syntax">`</span>' + | ||
const inlineCodeTransform = () => new SpanTransform({}, /\s`\S/, 3, /\S`\s/, 3, (span) => { | ||
const start = span.slice(0, 1); | ||
const end = span.slice(-1); | ||
const value = span.slice(2, -2); | ||
return start + '<code><span class="pm-syntax">`</span>' + | ||
value + | ||
'<span class="pm-syntax">`</span></code>'; | ||
'<span class="pm-syntax">`</span></code>' + | ||
end; | ||
}); | ||
const inlineLinkTransform = new SpanTransform({}, /http/, 4, /[A-Za-z0-9\-\.\_\~\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=][^A-Za-z0-9\-\.\_\~\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=]/, 2, function(span) { | ||
const inlineLinkTransform = () => new SpanTransform({}, /http/, 4, /[A-Za-z0-9\-\.\_\~\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=][^A-Za-z0-9\-\.\_\~\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=]/, 2, function(span) { | ||
const link = span.slice(0, -1); | ||
@@ -354,3 +391,3 @@ const endWith = span.slice(-1); | ||
const h1Transform = new LineTransform({}, /^#[^#].*/, /.*/, (content) => { | ||
const h1Transform = () => new LineTransform({}, /^#[^#].*/, /.*/, (content) => { | ||
const mod = content.slice(1, -1); | ||
@@ -360,3 +397,3 @@ return `<h1><span class="pm-syntax">#</span>${mod}</h1>\n`; | ||
const h2Transform = new LineTransform({}, /^##[^#].*/, /.*/, (content) => { | ||
const h2Transform = () => new LineTransform({}, /^##[^#].*/, /.*/, (content) => { | ||
const mod = content.slice(2, -1); | ||
@@ -366,3 +403,3 @@ return `<h2><span class="pm-syntax">##</span>${mod}</h2>\n`; | ||
const h3Transform = new LineTransform({}, /^###[^#].*/, /.*/, (content) => { | ||
const h3Transform = () => new LineTransform({}, /^###[^#].*/, /.*/, (content) => { | ||
const mod = content.slice(3, -1); | ||
@@ -372,3 +409,3 @@ return `<h2><span class="pm-syntax">###</span>${mod}</h3>\n`; | ||
const h4Transform = new LineTransform({}, /^####[^#].*/, /.*/, (content) => { | ||
const h4Transform = () => new LineTransform({}, /^####[^#].*/, /.*/, (content) => { | ||
const mod = content.slice(4, -1); | ||
@@ -378,3 +415,3 @@ return `<h2><span class="pm-syntax">####</span>${mod}</h4>\n`; | ||
const h5Transform = new LineTransform({}, /^#####[^#].*/, /.*/, (content) => { | ||
const h5Transform = () => new LineTransform({}, /^#####[^#].*/, /.*/, (content) => { | ||
const mod = content.slice(5, -1); | ||
@@ -384,3 +421,3 @@ return `<h5><span class="pm-syntax">#####</span>${mod}</h5>\n`; | ||
const horizontalRuleTransform = new LineTransform({}, /^ *(-|_|\*) *\1 *\1 *(\n|$)/, /.*/, (content) => { | ||
const horizontalRuleTransform = () => new LineTransform({}, /^ *(-|_|\*) *\1 *\1 *(\n|$)/, /.*/, (content) => { | ||
const mod = content.slice(0, -1); | ||
@@ -390,3 +427,3 @@ return `<span class="pm-syntax pm-hr">${mod}</span>\n`; | ||
const blockquoteTransform = new LineTransform({}, /^>.*/, /^[^>].*/, (content) => { | ||
const blockquoteTransform = () => new LineTransform({}, /^>.*/, /^[^>].*/, (content) => { | ||
const mod = content.slice(0, -1).split('\n').map((line) => { | ||
@@ -398,4 +435,3 @@ return line.replace(/^>/, '<span class="pm-syntax">></span>'); | ||
const codeTransform = new LineTransform({}, /^```/, /^```/, (content) => { | ||
const codeTransform = () => new LineTransform({}, /^```/, /^```/, (content) => { | ||
const lang = content.match(/```(\w*)\n/)[1]; | ||
@@ -413,6 +449,6 @@ | ||
let code = lines.join('\n') | ||
.replace(/\*/g, '*') | ||
.replace(/_/g, '_') | ||
.replace(/~/g, '˜') | ||
.replace(/`/g, '`'); | ||
.replace(/\*/g, '*') | ||
.replace(/_/g, '_') | ||
.replace(/~/g, '˜') | ||
.replace(/`/g, '`'); | ||
@@ -428,3 +464,3 @@ if (lang) { | ||
'<span class="pm-syntax">```</span>' + | ||
'</span>'; | ||
'</span>\n'; | ||
}); | ||
@@ -434,18 +470,18 @@ | ||
export default function getStream(inStream) { | ||
return inStream.pipe(codeTransform) | ||
.pipe(strongTransform) | ||
.pipe(emphasisTransform) | ||
.pipe(strikeTransform) | ||
.pipe(inlineCodeTransform) | ||
.pipe(inlineLinkTransform) | ||
.pipe(h1Transform) | ||
.pipe(h2Transform) | ||
.pipe(h3Transform) | ||
.pipe(h4Transform) | ||
.pipe(h5Transform) | ||
.pipe(horizontalRuleTransform) | ||
.pipe(blockquoteTransform) | ||
.pipe(new ReferenceLinkTransform()) | ||
.pipe(new PreTransform()); | ||
export default function getStream(inStream, onError) { | ||
return inStream.pipe(codeTransform()).on('error', onError) | ||
.pipe(strongTransform()).on('error', onError) | ||
.pipe(emphasisTransform()).on('error', onError) | ||
.pipe(strikeTransform()).on('error', onError) | ||
.pipe(inlineCodeTransform()).on('error', onError) | ||
.pipe(inlineLinkTransform()).on('error', onError) | ||
.pipe(h1Transform()).on('error', onError) | ||
.pipe(h2Transform()).on('error', onError) | ||
.pipe(h3Transform()).on('error', onError) | ||
.pipe(h4Transform()).on('error', onError) | ||
.pipe(h5Transform()).on('error', onError) | ||
.pipe(horizontalRuleTransform()).on('error', onError) | ||
.pipe(blockquoteTransform()).on('error', onError) | ||
.pipe(new ReferenceLinkTransform()).on('error', onError) | ||
.pipe(new PreTransform()).on('error', onError); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
56125
820
12