gitbook-markdown
Advanced tools
Comparing version 1.3.2 to 2.0.0
var _ = require('lodash'); | ||
var kramed = require('kramed'); | ||
var annotate = require('kramed/lib/annotate/'); | ||
var MarkupIt = require('markup-it'); | ||
var gitbookSyntax = require('markup-it/syntaxes/markdown'); | ||
var RAW_START = '{% raw %}'; | ||
var RAW_END = '{% endraw %}'; | ||
var RAW_END = '{% endraw %}'; | ||
var markdown = new MarkupIt(gitbookSyntax); | ||
/** | ||
Escape a code block's content using raw blocks | ||
@param {String} | ||
@return {String} | ||
*/ | ||
* Escape a code block's content using raw blocks | ||
* | ||
* @param {String} | ||
* @return {String} | ||
*/ | ||
function escape(str) { | ||
@@ -18,55 +19,46 @@ return RAW_START + str + RAW_END; | ||
/** | ||
Combines annotated nodes | ||
@param {Array} | ||
@return {String} | ||
*/ | ||
function combine(nodes) { | ||
return _.map(nodes, 'raw').join(''); | ||
} | ||
/** | ||
Add templating "raw" to code blocks to | ||
avoid nunjucks processing their content. | ||
@param {String} src | ||
@return {String} | ||
*/ | ||
* Add templating "raw" to code blocks to | ||
* avoid nunjucks processing their content. | ||
* | ||
* @param {String} src | ||
* @return {String} | ||
*/ | ||
function preparePage(src) { | ||
var lexed = annotate.blocks(src); | ||
var levelRaw = 0; | ||
var content = markdown.toContent(src, { | ||
math: true, | ||
template: true | ||
}); | ||
function escapeCodeElement(el) { | ||
if (el.type == 'code' && levelRaw == 0) { | ||
el.raw = escape(el.raw); | ||
} else if (el.type == 'tplexpr') { | ||
var expr = el.matches[0]; | ||
if(expr === 'raw') { | ||
levelRaw = levelRaw + 1; | ||
} else if(expr === 'endraw') { | ||
levelRaw = 0; | ||
} | ||
} | ||
return el; | ||
} | ||
var textMarkdown = markdown.toText(content, { | ||
annotate: function(state, raw, token) { | ||
var tokenType = token.getType(); | ||
var escaped = _.map(lexed, function(el) { | ||
// Only escape paragraphs and headings | ||
if(el.type == 'paragraph' || el.type == 'heading') { | ||
var line = annotate.inline(el.raw); | ||
if (tokenType === MarkupIt.ENTITIES.TEMPLATE) { | ||
var type = token.getData().get('type'); | ||
var expr = token.getAsPlainText(); | ||
// Escape inline code blocks | ||
line = line.map(escapeCodeElement); | ||
if (type === 'expr') { | ||
if (expr === 'raw') { | ||
levelRaw = levelRaw + 1; | ||
} else if (expr == 'endraw') { | ||
levelRaw = 0; | ||
} | ||
} | ||
} | ||
// Change raw source code | ||
el.raw = combine(line); | ||
if ( | ||
(tokenType === MarkupIt.BLOCKS.CODE || tokenType === MarkupIt.STYLES.CODE) | ||
&& levelRaw === 0 | ||
) { | ||
return escape(raw); | ||
} | ||
return el; | ||
} else { | ||
return escapeCodeElement(el); | ||
return raw; | ||
} | ||
}); | ||
return combine(escaped); | ||
return textMarkdown; | ||
} | ||
@@ -73,0 +65,0 @@ |
@@ -1,52 +0,32 @@ | ||
var _ = require('lodash'); | ||
var kramed = require('kramed'); | ||
var MarkupIt = require('markup-it'); | ||
var markdownSyntax = require('markup-it/syntaxes/markdown'); | ||
var htmlSyntax = require('markup-it/syntaxes/html'); | ||
/** | ||
Get renderer for kramed with correct configuration | ||
var markdown = new MarkupIt(markdownSyntax); | ||
var html = new MarkupIt(htmlSyntax); | ||
@return {kramed.Renderer} | ||
*/ | ||
function getRenderer() { | ||
return new kramed.Renderer({ | ||
langPrefix: 'lang-', | ||
smartypants: false, | ||
headerPrefix: '', | ||
headerAutoId: false, | ||
xhtml: false | ||
}); | ||
} | ||
/** | ||
Get options for markdown parsing | ||
@return {Object} | ||
*/ | ||
function getOption() { | ||
return _.extend({}, kramed.defaults, { | ||
mathjax: false | ||
}); | ||
} | ||
/** | ||
Convert Markdown block to HTML | ||
@param {String} src (markdown) | ||
@return {String} (html) | ||
*/ | ||
* Convert Markdown block to HTML | ||
* | ||
* @param {String} src (markdown) | ||
* @return {String} (html) | ||
*/ | ||
function convertMdToHTMLBlock(src) { | ||
var options = _.extend(getOption(), { | ||
renderer: getRenderer() | ||
}); | ||
var content = markdown.toContent(src); | ||
var textHtml = html.toText(content); | ||
return kramed(src, options); | ||
return textHtml; | ||
} | ||
/** | ||
Convert Markdown inline to HTML | ||
* Convert Markdown inline to HTML | ||
* | ||
* @param {String} src (markdown) | ||
* @return {String} (html) | ||
*/ | ||
function convertMdToHTMLInline(src) { | ||
var content = markdown.toInlineContent(src); | ||
var textHtml = html.toInlineText(content); | ||
@param {String} src (markdown) | ||
@return {String} (html) | ||
*/ | ||
function convertMdToHTMLInline(src) { | ||
return kramed.inlineLexer(src, getOption(), getRenderer()); | ||
return textHtml; | ||
} | ||
@@ -53,0 +33,0 @@ |
@@ -8,5 +8,5 @@ | ||
/* | ||
This module provides markup rules for gitbook-html | ||
These rules are being used to generate SUMMARY/GLOSSARY/LANGS | ||
*/ | ||
* This module provides markup rules for gitbook-html | ||
* These rules are being used to generate SUMMARY/GLOSSARY/LANGS | ||
*/ | ||
module.exports = { | ||
@@ -13,0 +13,0 @@ onTitleStart: function(level) { |
{ | ||
"name": "gitbook-markdown", | ||
"version": "1.3.2", | ||
"homepage": "https://www.gitbook.com", | ||
"description": "Parse markdown content for gitbook", | ||
"main": "lib/index.js", | ||
"dependencies": { | ||
"lodash": "^4.13.1", | ||
"kramed": "0.5.6", | ||
"kramed-text-renderer": "0.2.1", | ||
"gitbook-html": "1.3.3" | ||
"name": "gitbook-markdown", | ||
"version": "2.0.0", | ||
"homepage": "https://www.gitbook.com", | ||
"description": "Parse markdown content for gitbook", | ||
"main": "lib/index.js", | ||
"dependencies": { | ||
"gitbook-html": "1.3.3", | ||
"lodash": "^4.13.1", | ||
"markup-it": "^2.1.0 >=2.1.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^2.5.3" | ||
}, | ||
"scripts": { | ||
"test": "export TESTING=true; mocha --reporter list --bail" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/GitbookIO/gitbook-markdown.git" | ||
}, | ||
"author": "FriendCode Inc. <contact@gitbook.com>", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/GitbookIO/gitbook-markdown/issues" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Aaron O'Mullan", | ||
"email": "aaron@gitbook.com" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^2.5.3" | ||
}, | ||
"scripts": { | ||
"test": "export TESTING=true; mocha --reporter list --bail" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/GitbookIO/gitbook-markdown.git" | ||
}, | ||
"author": "FriendCode Inc. <contact@gitbook.com>", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/GitbookIO/gitbook-markdown/issues" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Aaron O'Mullan", | ||
"email": "aaron@gitbook.com" | ||
}, | ||
{ | ||
"name": "Samy Pessé", | ||
"email": "samy@gitbook.com" | ||
} | ||
] | ||
{ | ||
"name": "Samy Pessé", | ||
"email": "samy@gitbook.com" | ||
} | ||
] | ||
} |
@@ -7,3 +7,2 @@ var fs = require('fs'); | ||
describe('Page parsing', function() { | ||
@@ -22,12 +21,12 @@ var LEXED; | ||
it('should not add id to headings', function() { | ||
assert.equal(page('# Hello').content, '<h1>Hello</h1>\n'); | ||
assert.equal(page('# Hello {#test}').content, '<h1 id="test">Hello </h1>\n'); | ||
assert.equal(page('# Hello').content, '<h1>Hello</h1>'); | ||
assert.equal(page('# Hello {#test}').content, '<h1 id="test">Hello</h1>'); | ||
}); | ||
it('should escape codeblocks in preparation (1)', function() { | ||
assert.equal(page.prepare("Hello `world`"), "Hello {% raw %}`world`{% endraw %}"); | ||
assert.equal(page.prepare("Hello `world test`"), "Hello {% raw %}`world test`{% endraw %}"); | ||
assert.equal(page.prepare("Hello ```world test```"), "Hello {% raw %}```world test```{% endraw %}"); | ||
assert.equal(page.prepare("Hello\n```js\nworld test\n```\n"), "Hello\n{% raw %}```js\nworld test\n```\n{% endraw %}"); | ||
assert.equal(page.prepare("Hello\n```\ntest\n\tworld\n\ttest\n```"), "Hello\n{% raw %}```\ntest\n\tworld\n\ttest\n```{% endraw %}"); | ||
assert.equal(page.prepare("Hello `world`"), 'Hello {% raw %}`world`{% endraw %}\n\n'); | ||
assert.equal(page.prepare("Hello `world test`"), 'Hello {% raw %}`world test`{% endraw %}\n\n'); | ||
assert.equal(page.prepare("Hello ```world test```"), 'Hello {% raw %}`world test`{% endraw %}\n\n'); | ||
assert.equal(page.prepare("Hello\n```js\nworld test\n```\n"), 'Hello\n\n{% raw %}```js\nworld test\n```\n\n{% endraw %}'); | ||
assert.equal(page.prepare("Hello\n```\ntest\n\tworld\n\ttest\n```"), 'Hello\n\n{% raw %}```\ntest\n world\n test\n```\n\n{% endraw %}'); | ||
}); | ||
@@ -38,7 +37,7 @@ | ||
page.prepare("Hello\n\n\n\tworld\n\thello\n\n\ntest"), | ||
"Hello\n\n\n{% raw %}\tworld\n\thello\n\n\n{% endraw %}test" | ||
'Hello\n\n{% raw %}```\nworld\nhello```\n\n{% endraw %}test\n\n' | ||
); | ||
assert.equal( | ||
page.prepare("Hello\n\n\n\tworld\n\thello\n\n\n"), | ||
"Hello\n\n\n{% raw %}\tworld\n\thello\n\n\n{% endraw %}" | ||
'Hello\n\n{% raw %}```\nworld\nhello```\n\n{% endraw %}' | ||
); | ||
@@ -50,3 +49,3 @@ }); | ||
page.prepare('Hello {{ "Bonjour" }} ```test```'), | ||
'Hello {{ "Bonjour" }} {% raw %}```test```{% endraw %}' | ||
'Hello {{ "Bonjour" }} {% raw %}`test`{% endraw %}\n\n' | ||
); | ||
@@ -58,11 +57,11 @@ }); | ||
page.prepare('{% raw %}Hello {{ "Bonjour" }} ```test```{% endraw %}'), | ||
'{% raw %}Hello {{ "Bonjour" }} ```test```{% endraw %}' | ||
'{% raw %}Hello {{ "Bonjour" }} `test`{% endraw %}\n\n' | ||
); | ||
assert.equal( | ||
page.prepare('{% raw %}Hello {{ "Bonjour" }} {% raw %}{% endraw %}```test```'), | ||
'{% raw %}Hello {{ "Bonjour" }} {% raw %}{% endraw %}{% raw %}```test```{% endraw %}' | ||
'{% raw %}Hello {{ "Bonjour" }} {% raw %}{% endraw %}{% raw %}`test`{% endraw %}\n\n' | ||
); | ||
assert.equal( | ||
page.prepare('```{% raw %}Hello {{ "Bonjour" }} {% raw %}```'), | ||
'{% raw %}```{% raw %}Hello {{ "Bonjour" }} {% raw %}```{% endraw %}' | ||
'{% raw %}`{% raw %}Hello {{ "Bonjour" }} {% raw %}`{% endraw %}\n\n' | ||
); | ||
@@ -72,3 +71,3 @@ | ||
page.prepare('```\ntest\n```\n\n\n### Test'), | ||
'{% raw %}```\ntest\n```\n{% endraw %}\n\n### Test' | ||
'{% raw %}```\ntest\n```\n\n{% endraw %}### Test\n\n' | ||
); | ||
@@ -78,5 +77,5 @@ }); | ||
it('should not process math', function() { | ||
assert.equal(page.prepare("Hello $world$"), "Hello $world$"); | ||
assert.equal(page.prepare("Hello $$world$$"), "Hello $$world$$"); | ||
assert.equal(page.prepare("Hello $world$"), 'Hello $world$\n\n'); | ||
assert.equal(page.prepare("Hello $$world$$"), 'Hello $$world$$\n\n'); | ||
}); | ||
}); |
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
3
15615
341
+ Addedmarkup-it@^2.1.0 >=2.1.0
+ Addedansi-regex@2.1.1(transitive)
+ Addedcamelcase@3.0.0(transitive)
+ Addedcliui@3.2.0(transitive)
+ Addedcode-point-at@1.1.0(transitive)
+ Addeddecamelize@1.2.0(transitive)
+ Addederror-ex@1.3.2(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedfind-up@1.1.2(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-caller-file@1.0.3(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhosted-git-info@2.8.9(transitive)
+ Addedhtml-entities@1.4.0(transitive)
+ Addedhtmlparser2@3.10.1(transitive)
+ Addedimmutable@3.8.2(transitive)
+ Addedinvert-kv@1.0.0(transitive)
+ Addedis@3.3.0(transitive)
+ Addedis-arrayish@0.2.1(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addedis-fullwidth-code-point@1.0.0(transitive)
+ Addedis-utf8@0.2.1(transitive)
+ Addedlcid@1.0.0(transitive)
+ Addedload-json-file@1.1.0(transitive)
+ Addedlodash.assign@4.2.0(transitive)
+ Addedltrim@0.0.3(transitive)
+ Addedmarkup-it@2.5.0(transitive)
+ Addednormalize-package-data@2.5.0(transitive)
+ Addednumber-is-nan@1.0.1(transitive)
+ Addedobject-values@1.0.0(transitive)
+ Addedos-locale@1.4.0(transitive)
+ Addedparse-json@2.2.0(transitive)
+ Addedpath-exists@2.1.0(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedpath-type@1.1.0(transitive)
+ Addedpify@2.3.0(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedpsl@1.14.0(transitive)
+ Addedrange-utils@1.1.0(transitive)
+ Addedread-pkg@1.1.0(transitive)
+ Addedread-pkg-up@1.0.1(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedrequire-directory@2.1.1(transitive)
+ Addedrequire-main-filename@1.0.1(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedrtrim@0.0.3(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedspdx-correct@3.2.0(transitive)
+ Addedspdx-exceptions@2.5.0(transitive)
+ Addedspdx-expression-parse@3.0.1(transitive)
+ Addedspdx-license-ids@3.0.20(transitive)
+ Addedstring-width@1.0.2(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedstrip-bom@2.0.0(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedvalidate-npm-package-license@3.0.4(transitive)
+ Addedwhich-module@1.0.0(transitive)
+ Addedwindow-size@0.2.0(transitive)
+ Addedwrap-ansi@2.1.0(transitive)
+ Addedy18n@3.2.2(transitive)
+ Addedyargs@4.8.1(transitive)
+ Addedyargs-parser@2.4.1(transitive)
- Removedkramed@0.5.6
- Removedkramed-text-renderer@0.2.1
- Removedkramed@0.5.6(transitive)
- Removedkramed-text-renderer@0.2.1(transitive)
- Removedpsl@1.13.0(transitive)