Skip to content

Commit

Permalink
Merge pull request markedjs#456 from Feder1co5oave/clean-segments
Browse files Browse the repository at this point in the history
IDs of headings with inline tokens (link, em, bold...)
  • Loading branch information
joshbruce authored Jan 20, 2018
2 parents 7a55127 + b5e3b88 commit 648608d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
33 changes: 31 additions & 2 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1307,6 +1335,7 @@ marked.Parser = Parser;
marked.parser = Parser.parse;

marked.Renderer = Renderer;
marked.TextRenderer = TextRenderer;

marked.Lexer = Lexer;
marked.lexer = Lexer.lex;
Expand Down
13 changes: 13 additions & 0 deletions test/new/headings-id.html
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions test/new/headings-id.md
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion test/new/links_reference_style.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 648608d

Please sign in to comment.