-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom renderer for parsing markdown documents
- Loading branch information
Showing
3 changed files
with
124 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
var md = require('marked'); | ||
|
||
var renderer; | ||
|
||
/** | ||
* Creates custom renderer for markdown parsing | ||
* @returns {marked.Renderer} | ||
*/ | ||
function createRenderer() { | ||
renderer = new md.Renderer(); | ||
|
||
/** | ||
* Fix marked issue with cyrillic symbols replacing | ||
* @param text - {String} test of header | ||
* @param level - {Number} index of header | ||
* @param raw | ||
* @param options - {Object} options | ||
* @returns {String} - result header string | ||
*/ | ||
renderer.heading = function(text, level, raw, options) { | ||
var specials = ['-','[',']','/','{','}','(',')','*','+','?','.','\\','^','$','|','\ ','\'','\"']; | ||
|
||
options = options || {}; | ||
options.headerPrefix = options.headerPrefix || ''; | ||
|
||
return '<h' + level + ' id="' + options.headerPrefix | ||
+ raw.replace(RegExp('[' + specials.join('\\') + ']', 'g'), '-') + '">' | ||
+ text + '</h' + level + '>\n'; | ||
}; | ||
|
||
return renderer; | ||
} | ||
|
||
exports.getRenderer = function() { | ||
return renderer || createRenderer(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters