mirror of
https://github.com/jsdoc/jsdoc.git
synced 2025-12-08 19:46:11 +00:00
Moved underscore escaping to a dedicated function, fixed typo in test comment (evilstreak -> gfm)
This commit is contained in:
parent
0973e636fc
commit
280d98f00a
@ -21,6 +21,20 @@ var parsers = {
|
||||
gfm: "github-flavored-markdown"
|
||||
};
|
||||
|
||||
/**
|
||||
* Escape underscores that occur within {@ ... } in order to protect them
|
||||
* from the markdown parser(s).
|
||||
* @param {String} source the source text to sanitize.
|
||||
* @returns {String} `source` where underscores within {@ ... } have been
|
||||
* protected with a preceding backslash (i.e. \_) -- the markdown parsers
|
||||
* will strip the backslash and protect the underscore.
|
||||
*/
|
||||
function escapeUnderscores(source) {
|
||||
return source.replace(/\{@[^}\r\n]+\}/g, function (wholeMatch) {
|
||||
return wholeMatch.replace(/(^|[^\\])_/g, '$1\\_');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a function that accepts a single parameter containing Markdown source. The function uses
|
||||
* the specified parser to transform the Markdown source to HTML, then returns the HTML as a string.
|
||||
@ -48,11 +62,7 @@ function getParseFunction(parser, conf) {
|
||||
parser.hardwrap = !!conf.hardwrap;
|
||||
|
||||
return function(source) {
|
||||
// 1. protect underscores within inline tags {@....}
|
||||
source = source.replace(/\{@[^}\r\n]+\}/g, function (wholeMatch) {
|
||||
return wholeMatch.replace(/(^|[^\\])_/g, '$1\\_');
|
||||
});
|
||||
// 2. send through markdown (the protective '\' will be removed
|
||||
source = escapeUnderscores(source);
|
||||
// by the parser)
|
||||
return parser.parse(source, githubConf);
|
||||
};
|
||||
@ -60,12 +70,7 @@ function getParseFunction(parser, conf) {
|
||||
parser = require(parser).markdown;
|
||||
|
||||
return function(source) {
|
||||
// 1. protect underscores within inline tags {@....}
|
||||
source = source.replace(/\{@[^}\r\n]+\}/g, function (wholeMatch) {
|
||||
return wholeMatch.replace(/(^|[^\\])_/g, '$1\\_');
|
||||
});
|
||||
// 2. send through markdown (the protective '\' will be removed
|
||||
// by the parser)
|
||||
source = escapeUnderscores(source);
|
||||
// evilstreak parser expects line endings to be \n
|
||||
source = source.replace(/\r\n|\r/g, '\n');
|
||||
return parser.toHTML(source, conf.dialect);
|
||||
|
||||
@ -52,7 +52,7 @@ describe('jsdoc/util/markdown', function() {
|
||||
var old = (env.conf.markdown ? env.conf.markdown.parser : undefined);
|
||||
env.conf.markdown = {parser: 'gfm'};
|
||||
|
||||
// get the evilstreak parser and do the test
|
||||
// get the gfm parser and do the test
|
||||
var parser = markdown.getParser();
|
||||
expect(parser('{@link MyClass#_x} and {@link MyClass#_y}')).toEqual(
|
||||
'<p>{@link MyClass#_x} and {@link MyClass#_y}</p>');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user