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

bem-md-renderer

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bem-md-renderer - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

sandbox/article-en.md

118

index.js
var marked = require('marked'),
renderer,
usedAnchors = {};
_ = require('lodash'),
usedAnchors = {},
renderer;
/**
* Returns an anchor for a given header
* @param {String} headerText
* @returns {String}
* Render html from markdown with custom bem-site renderer
* @param {String} markdown(required) - source md
* @param {Options} options - custom options to marked module
* @param {Function} cb(required) - callback function
*/
function getAnchor(headerText) {
var anchor = headerText.replace(/( )/g, '-'),
allowedChars = new RegExp('[A-Za-zА-Яа-яЁё0-9_\\- ]', 'g');
function render(markdown, options, cb) {
var args = Array.prototype.slice.call(arguments);
anchor = anchor.match(allowedChars) || [];
// check markdown string
if (!markdown) throw new Error('Markdown string should be passed in arguments');
if (!_.isString(markdown)) throw new Error('Markdown must be a string');
var _anchor = '';
for (var i = 0; i < anchor.length; i++) {
_anchor += anchor[i].match(/[A-Z]/) ? anchor[i].toLowerCase() : anchor[i];
// check arguments length
if (args.length === 3) {
if (!_.isObject(options)) throw new Error('Options must be an object');
if (!_.isFunction(cb)) throw new Error('Callback must be a function');
} else {
if (!_.isFunction(arguments[1])) {
throw new Error('If the options is not passed, the second argument must be a callback function');
}
// set requires variables
options = {};
cb = args[1];
}
return _anchor;
// render html from markdown
marked(markdown, _.extend({
gfm: true,
pedantic: false,
sanitize: false,
renderer: getRenderer()
}, options), function (err, result) {
if (err) return cb(err);
return cb(null, result);
});
}
/**
* Create instance of new Marked renderer
* Contain custom renderer for:
* 1. Heading, GitHub style with anchors and links inside
* 2. Table for fix trouble with page scroll, when table is so wide
* @returns {*|Renderer}
*/
function createRenderer() {

@@ -43,9 +72,4 @@ renderer = new marked.Renderer();

anchor = options.headerPrefix + getAnchor(raw);
anchor = modifyDuplicate(anchor);
if (usedAnchors.hasOwnProperty(anchor)) {
anchor += '-' + usedAnchors[anchor]++;
} else {
usedAnchors[anchor] = 1;
}
return '<h' + level + ' id="' + anchor + '"><a href="#' + anchor + '" class="anchor"></a>' +

@@ -78,4 +102,54 @@ text + '</h' + level + '>\n';

module.exports.getRenderer = function () {
return renderer || createRenderer();
};
/**
* Return an instance of custom marked renderer
* Reset usedAnchors variable,
* which need to check duplicate headers in the markdown
* @returns {*}
*/
function getRenderer() {
if (!renderer) renderer = createRenderer();
usedAnchors = {};
return renderer;
}
/**
* Returns an anchor for a given header
* @param {String} headerText -> 'BEM templates' -> 'BEM-templates'
* @returns {String}
*/
function getAnchor(headerText) {
var anchor = headerText.replace(/( )/g, '-'),
allowedChars = new RegExp('[A-Za-zА-Яа-яЁё0-9_\\- ]', 'g');
anchor = anchor.match(allowedChars) || [];
var _anchor = '';
for (var i = 0; i < anchor.length; i++) {
_anchor += anchor[i].match(/[A-Z]/) ? anchor[i].toLowerCase() : anchor[i];
}
return _anchor;
}
/**
* Modify duplicate headers on the page by GitHub rules
* For example: we found two identical header: examples and examples
* In this case, to make the anchors on these different headers,
* we need add to second header anchor him count, e.g. examples-1
* @param {String} anchor source anchor
* @returns {String} modify anchor
*/
function modifyDuplicate(anchor) {
if (usedAnchors.hasOwnProperty(anchor)) {
anchor += '-' + usedAnchors[anchor]++;
} else {
usedAnchors[anchor] = 1;
}
return anchor;
}
exports.getRenderer = getRenderer;
exports.getAnchor = getAnchor;
exports.render = render;

7

package.json
{
"name": "bem-md-renderer",
"version": "0.0.1",
"description": "Markdown renderer for Github like anchors. Use on bem-sites with npm module marked.js",
"version": "0.1.0",
"description": "The builder markdown to html with custom renderer, which generates the html headlines like github",
"main": "index.js",

@@ -17,3 +17,3 @@ "repository": {

"engines": {
"node": ">= 0.10.0"
"node": ">= 0.10"
},

@@ -25,2 +25,3 @@ "bugs": {

"dependencies": {
"lodash": "^3.3.1",
"marked": "~0.3.2"

@@ -27,0 +28,0 @@ },

# bem-md-renderer
Markdown renderer for Github like anchors. Use on bem-sites with npm module https://github.com/chjj/marked
## Usage
Minimal usage:
```
var bmdr = require('bem-md-renderer');
bmdr.render('I am using __markdown__.', function(err, result) {
if(err) throw err;
console.log('result', result);
});
// Outputs: <p>I am using <strong>markdown</strong>.</p>
```
## API
## bmdr.render(markdownString [,options], callback);
### markdownString
Type: `string`
Required. String of markdown source to be compiled.
### options
Type: `object`
Hash of options. All available options can be viewed in `marked` module docs https://github.com/chjj/marked
### callback
Type: `function`
Required. Function called when the `markdownString` has been fully parsed when using
async highlighting. If the `options` argument is omitted, this can be used as
the second argument.
## bmdr.getRenderer();
Return an instance of custom marked renderer.
Example:
```
var marked = require('marked'),
bmdr = required('bem-md-renderer');
var html = marked('## Some title\n## Some title', {
gfm: true,
pedantic: false,
sanitize: false,
renderer: bmdr.getRenderer() // get custom renderer
});
console.log(html);
/**
* Outputs:
* <h2 id="some-title"><a href="#some-title"></a>some-title</h2>
* <h2 id="some-title-1"><a href="#some-title-1"></a>some-title</h2>
*/
```
**Note #1:** The titles are identical, but both has a different anchors
**Note #2:** You can make possible a copy anchor by click on `<a>` inside headline, when you hover it
**Note #3:** Work good with TOC https://github.com/eGavr/toc-md
## bmdr.getAnchor(anchorString);
Return an anchor, create in Github styles
### anchorString
Type: `string`
Required. String of headline text. Work with latin and cyrilic symbols
Example:
```
var bmdr = require('bem-md-renderer');
console.log(bmdr.getAnchor('Create a decl for a "heavy" block requested by demand'));
// Outputs: create-a-decl-for-a-heavy-block-requested-by-demand
```
var marked = require('marked'),
should = require('should'),
should = require('should');

@@ -4,0 +4,0 @@ renderer = require('../index.js'),

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