Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable heading IDs #1190

Merged
merged 15 commits into from
Apr 3, 2018
25 changes: 15 additions & 10 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -823,16 +823,20 @@ Renderer.prototype.html = function(html) {
};

Renderer.prototype.heading = function(text, level, raw) {
return '<h'
+ level
+ ' id="'
+ this.options.headerPrefix
+ raw.toLowerCase().replace(/[^\w]+/g, '-')
+ '">'
+ text
+ '</h'
+ level
+ '>\n';
if (this.options.headerIds) {
return '<h'
+ level
+ ' id="'
+ this.options.headerPrefix
+ raw.toLowerCase().replace(/[^\w]+/g, '-')
+ '">'
+ text
+ '</h'
+ level
+ '>\n';
}
// ignore IDs
return '<h' + level + '>' + text + '</h' + level + '>\n';
};

Renderer.prototype.hr = function() {
Expand Down Expand Up @@ -1355,6 +1359,7 @@ marked.defaults = {
langPrefix: 'lang-',
smartypants: false,
headerPrefix: '',
headerIds: true,
renderer: new Renderer(),
xhtml: false,
baseUrl: null
Expand Down
2 changes: 1 addition & 1 deletion marked.min.js

Large diffs are not rendered by default.

169 changes: 169 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
"eslint-plugin-standard": "^3.0.1",
"front-matter": "^2.3.0",
"glob-to-regexp": "0.3.0",
"html-differ": "^1.3.4",
"jasmine": "^3.1.0",
"jasmine2-custom-message": "^0.9.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are not needed for this PR

"markdown": "*",
"markdown-it": "*",
"showdown": "*",
Expand Down
45 changes: 45 additions & 0 deletions test/integration/marked-spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
var marked = require('../../marked.min.js');
var HtmlDiffer = require('html-differ').HtmlDiffer,
htmlDiffer = new HtmlDiffer();
var since = require('jasmine2-custom-message');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not needed


it('should run the test', function () {
expect(marked('Hello World!')).toBe('<p>Hello World!</p>\n');
Expand All @@ -16,3 +19,45 @@ it('indents code within an explicitly-started ordered list', function () {
expect(marked(" 10. foo\n\n bar")).
toBe("<ol start=\"10\">\n<li><p>foo</p>\n<pre><code>bar\n</code></pre></li>\n</ol>\n");
});

// it('should be able to change options', function() {
// marked.setOptions({ headerIds: false });
// since(JSON.stringify(marked.options)).expect(marked.options.headerIds).toBe(false);
// });

it('should add header ID by default', function () {
marked.setOptions({ headerIds: true });

var markdown = '# hello';
var expected = '<h1 id="hello">hello</h1>';
var actual = marked(markdown);

var message = 'Default options adds header id:\n'
+ markdown
+ '\n------\n\nExpected:\n' + expected
+ '\n------\n\nMarked:\n' + actual;

var diff = htmlDiffer.isEqual(expected, actual);

since(message).expect(diff).toBe(true);

});

it('should NOT add header ID when option is false', function () {
marked.setOptions({ headerIds: false });

var markdown = '# hello';
var expected = '<h1>hello</h1>';
var actual = marked(markdown);

var message = 'Custom options does NOT adds header id:\n'
+ markdown
+ '\n------\n\nExpected:\n' + expected
+ '\n------\n\nMarked:\n' + actual + '\n\n'
+ JSON.stringify(marked.options);

var diff = htmlDiffer.isEqual(expected, actual);

since(message).expect(diff).toBe(true);

});