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

Provide anchors with headings if Github flowered markdown #131

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,9 +820,22 @@ Parser.prototype.tok = function() {
return '<hr>\n';
}
case 'heading': {
var internal = '';
if (this.options.gfm) {
var link = this.token.text
.toLowerCase()
.replace(/\s/g, '-')
.replace(/[^a-z0-9-_\:\.]/g, '');
Copy link

Choose a reason for hiding this comment

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

Note that including .s and :s will break CSS selecting.

Copy link
Author

Choose a reason for hiding this comment

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

how does this break selecting? link is only used as href and name

Copy link

Choose a reason for hiding this comment

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

Some (bootstrap's scrollspy for example) might want to select elements like this: a[href=the-converted-id]

viz. http://stackoverflow.com/a/72577, also a bit surprisingly, name is supposed to be deprecated: http://www.w3schools.com/tags/att_a_name.asp

Copy link
Author

Choose a reason for hiding this comment

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

I can see this being the case for general use cases, however this
addition is specifically tailored to Github Flavored Markdown, and as
such following their conventions. Also note that it's using the heading
text not the id. I can see the scrollspy working like this, but if I'm
missing something tell me please

On 03/27/2013 06:40 PM, Michal Srb wrote:

In lib/marked.js:

@@ -820,9 +820,22 @@ Parser.prototype.tok = function() {
return '


\n';
}
case 'heading': {

  •  var internal = '';
    
  •  if (this.options.gfm) {
    
  •    var link = this.token.text
    
  •      .toLowerCase()
    
  •      .replace(/\s/g, '-')
    
  •      .replace(/[^a-z0-9-_:.]/g, '');
    

Some (bootstrap's scrollspy for example) might want to select elements
like this: a[href=the-converted-id]


Reply to this email directly or view it on GitHub
https://github.com/chjj/marked/pull/131/files#r3555907.


// Check if ID starts with a letter, otherwise make sure it does.
link = /^[a-z]/.test(link) ? link : 'a-' + link;
internal = '<a class="anchor" href="#' + link + '" id="' + link + '"></a>';
}

return '<h'
+ this.token.depth
+ '>'
+ internal
+ this.inline.output(this.token.text)
+ '</h'
+ this.token.depth
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "marked",
"description": "A markdown parser built for speed",
"author": "Christopher Jeffrey",
"version": "0.2.8",
"version": "0.2.9",
"main": "./lib/marked.js",
"bin": "./bin/marked",
"man": "./man/marked.1",
Expand Down