Disable heading IDs (#1190)

* Add option to disable heading ids
* Alphabetize and add options to docs
This commit is contained in:
Josh Bruce 2018-04-03 15:10:26 -04:00 committed by Steven
parent 9a52de7a2c
commit 8804676f98
4 changed files with 61 additions and 35 deletions

View File

@ -39,18 +39,25 @@ console.log(myMarked('I am using __markdown__.'));
<h2 id="options">Options</h2> <h2 id="options">Options</h2>
|Member |Type |Notes | |Member |Type |Notes |
|:----------|:---------|:----------------------------------------------------------------------------------------------------------------------------| |:-----------|:---------|:----------------------------------------------------------------------------------------------------------------------------|
|highlight |`function`|A function to highlight code blocks. See also: <a href="#highlight">Asynchronous highlighting</a>.| |baseUrl |`??` |Default is `null` |
|renderer |`object` |An object containing functions to render tokens to HTML. See [extensibility](https://github.com/markedjs/marked/blob/master/docs/USING_PRO.md) for more details. Default: `new Renderer()`.| |breaks |`boolean` |Use GFM [hard](https://github.github.com/gfm/#hard-line-breaks) and [soft](https://github.github.com/gfm/#soft-line-breaks) line breaks. Requires `gfm` be `true`. Default: `false`|
|pedantic |`boolean` |Conform to obscure parts of `markdown.pl` as much as possible. Don't fix original markdown bugs or behavior. Default: `false`.| |gfm |`boolean` |Use approved [GitHub Flavored Markdown (GFM) specification](https://github.github.com/gfm/). |
|gfm |`boolean` |Use approved [GitHub Flavored Markdown (GFM) specification](https://github.github.com/gfm/). Default: `true`.| |headerIds |`boolean` |Whether to add an `id` attribute to headers. Default: `true` |
|tables |`boolean` |Use [GFM Tables extension](https://github.github.com/gfm/#tables-extension-). Requires `gfm` be `true`. Default: `true`.| |headerPrefix|`string` |A short string to add as a prefix to the `id` attributes added to headers by default. Default: `empty string` |
|breaks |`boolean` |Use GFM [hard](https://github.github.com/gfm/#hard-line-breaks) and [soft](https://github.github.com/gfm/#soft-line-breaks) line breaks. Requires `gfm` be `true`. Default: `false`.| |highlight |`function`|A function to highlight code blocks. See also: <a href="#highlight">Asynchronous highlighting</a>. |
|sanitize |`boolean` |Ignore HTML passed into `markdownString` (sanitize the input). Default: `false`.| |langPrefix |`??` |Default is `lang-`
|smartlists |`boolean` |Use smarter list behavior than those found in `markdown.pl`. Default: `true`.| |mangle |`boolean` |Default is `true`
|smartypants|`boolean` |Use "smart" typographic punctuation for things like quotes and dashes. Default: `false`.| |pedantic |`boolean` |Conform to obscure parts of `markdown.pl` as much as possible. Don't fix original markdown bugs or behavior. Default: `false`|
|xhtml |`boolean` |Self-close the tags for void elements (&lt;br/&gt;, &lt;img/&gt;, etc.) with a "/" as required by XHTML. Default: `false`.| |renderer |`object` |An object containing functions to render tokens to HTML. See [extensibility](https://github.com/markedjs/marked/blob/master/docs/USING_PRO.md) for more details. Default: `new Renderer()`|
|sanitize |`boolean` |Ignore HTML passed into `markdownString` (sanitize the input). Default: `false` |
|sanitizer |`??` |Default is `null` |
|silent |`boolean` |Default is `false` |
|smartlists |`boolean` |Use smarter list behavior than those found in `markdown.pl`. Default: `true` |
|smartypants |`boolean` |Use "smart" typographic punctuation for things like quotes and dashes. |
|tables |`boolean` |Use [GFM Tables extension](https://github.github.com/gfm/#tables-extension-). Requires `gfm` be `true`. |
|xhtml |`boolean` |Self-close the tags for void elements (&lt;br/&gt;, &lt;img/&gt;, etc.) with a "/" as required by XHTML. Default: `false` |
<h2 id="highlight">Asynchronous highlighting</h2> <h2 id="highlight">Asynchronous highlighting</h2>

View File

@ -823,16 +823,20 @@ Renderer.prototype.html = function(html) {
}; };
Renderer.prototype.heading = function(text, level, raw) { Renderer.prototype.heading = function(text, level, raw) {
return '<h' if (this.options.headerIds) {
+ level return '<h'
+ ' id="' + level
+ this.options.headerPrefix + ' id="'
+ raw.toLowerCase().replace(/[^\w]+/g, '-') + this.options.headerPrefix
+ '">' + raw.toLowerCase().replace(/[^\w]+/g, '-')
+ text + '">'
+ '</h' + text
+ level + '</h'
+ '>\n'; + level
+ '>\n';
}
// ignore IDs
return '<h' + level + '>' + text + '</h' + level + '>\n';
}; };
Renderer.prototype.hr = function() { Renderer.prototype.hr = function() {
@ -1342,22 +1346,23 @@ marked.setOptions = function(opt) {
}; };
marked.defaults = { marked.defaults = {
gfm: true, baseUrl: null,
tables: true,
breaks: false, breaks: false,
pedantic: false, gfm: true,
sanitize: false, headerIds: true,
sanitizer: null, headerPrefix: '',
mangle: true,
smartLists: false,
silent: false,
highlight: null, highlight: null,
langPrefix: 'lang-', langPrefix: 'lang-',
smartypants: false, mangle: true,
headerPrefix: '', pedantic: false,
renderer: new Renderer(), renderer: new Renderer(),
xhtml: false, sanitize: false,
baseUrl: null sanitizer: null,
silent: false,
smartLists: false,
smartypants: false,
tables: true,
xhtml: false
}; };
/** /**

2
marked.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -5,3 +5,17 @@ it('should run the test', function () {
marked.parse('Hello World!'); marked.parse('Hello World!');
expect(marked.parse).toHaveBeenCalled(); expect(marked.parse).toHaveBeenCalled();
}); });
describe('Test heading ID functionality', function() {
it('should add id attribute by default', function() {
var renderer = new marked.Renderer(marked.defaults);
var header = renderer.heading('test', 1, 'test');
expect(header).toBe('<h1 id="test">test</h1>\n');
});
it('should NOT add id attribute when options set false', function() {
var renderer = new marked.Renderer({ headerIds: false });
var header = renderer.heading('test', 1, 'test');
expect(header).toBe('<h1>test</h1>\n');
});
});