Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gitbook-markdown

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gitbook-markdown - npm Package Compare versions

Comparing version 1.3.2 to 2.0.0

90

lib/page.js
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');
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc