Merge pull request #456 from Feder1co5oave/clean-segments

IDs of headings with inline tokens (link, em, bold...)
This commit is contained in:
Josh Bruce 2018-01-20 16:09:27 -05:00 committed by GitHub
commit 19f6afe4aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 3 deletions

View File

@ -920,6 +920,32 @@ Renderer.prototype.text = function(text) {
return text;
};
/**
* TextRenderer
* returns only the textual part of the token
*/
function TextRenderer() {}
// no need for block level renderers
TextRenderer.prototype.strong =
TextRenderer.prototype.em =
TextRenderer.prototype.codespan =
TextRenderer.prototype.del =
TextRenderer.prototype.text = function (text) {
return text;
}
TextRenderer.prototype.link =
TextRenderer.prototype.image = function(href, title, text) {
return '' + text;
}
TextRenderer.prototype.br = function() {
return '';
}
/**
* Parsing & Compiling
*/
@ -947,7 +973,9 @@ Parser.parse = function(src, options) {
*/
Parser.prototype.parse = function(src) {
this.inline = new InlineLexer(src.links, this.options, this.renderer);
this.inline = new InlineLexer(src.links, this.options);
// use an InlineLexer with a TextRenderer to extract pure text
this.inlineText = new InlineLexer(src.links, merge({}, this.options, {renderer: new TextRenderer}));
this.tokens = src.reverse();
var out = '';
@ -1004,7 +1032,7 @@ Parser.prototype.tok = function() {
return this.renderer.heading(
this.inline.output(this.token.text),
this.token.depth,
this.token.text);
unescape(this.inlineText.output(this.token.text)));
}
case 'code': {
return this.renderer.code(this.token.text,
@ -1307,6 +1335,7 @@ marked.Parser = Parser;
marked.parser = Parser.parse;
marked.Renderer = Renderer;
marked.TextRenderer = TextRenderer;
marked.Lexer = Lexer;
marked.lexer = Lexer.lex;

13
test/new/headings-id.html Normal file
View File

@ -0,0 +1,13 @@
<h3 id="heading-with-a-link">Heading with a <a href="http://github.com/">link</a></h3>
<h3 id="heading-with-some-italic-text">Heading with some <em>italic text</em></h3>
<h3 id="or-some-strong">Or some <strong>strong</strong></h3>
<p>(which doesn&#39;t really make any difference, here)</p>
<h3 id="or-even-code">Or even <code>code</code></h3>
<h3 id="what-about-strikethrough">What about <del>strikethrough</del></h3>
<h2 id="and-a-ref-link">And a ref <a href="/some/url" title="link to nowhere">link</a></h2>

14
test/new/headings-id.md Normal file
View File

@ -0,0 +1,14 @@
### Heading with a [link](http://github.com/)
### Heading with some _italic text_
### Or some **strong**
(which doesn't really make any difference, here)
### Or even `code`
### What about ~~strikethrough~~
## And a ref [link][destination]
[destination]: /some/url "link to nowhere"

View File

@ -48,7 +48,7 @@
<p>[bar]</p>
<p>However, it can directly follow other block elements, such as headings</p>
<h1 id="-foo-"><a href="/url">Foo</a></h1>
<h1 id="foo"><a href="/url">Foo</a></h1>
<blockquote>
<p>bar</p>
</blockquote>