Skip to content

Commit

Permalink
Added automatic anchors for headers. Fulfils issue #93.
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-p committed Oct 5, 2013
1 parent 01bc000 commit 83aa9c4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/common/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ Change Log
* Now works very well with **Wordpress** ([details](https://github.com/adam-p/markdown-here/wiki/Compatibility#wordpress)).
* See the [Compatibility wiki page](https://github.com/adam-p/markdown-here/wiki/Compatibility) for even more places where Markdown Here works, like **Google Sites** and **Facebook Notes**.

* New feature: Automatic anchors for headers. This makes it much easier to put a **table of contents** (or other intra-page links) inside your email or blog post. Just use the text of your header as the anchor link text. For example:
```no-highlight
[See below](#Deep-Dive Details Section) for details.
...
Deep-Dive Details Section
=========================
...
```
* Thanks to [Casey Watts](https://github.com/caseywatts) for requesting this and giving insight on how to do it. Closes issue [#93](https://github.com/adam-p/markdown-here/issues/93).
* [Fixed bug](https://github.com/adam-p/markdown-here/issues/84): Math: single-character formula won't render.
* Thanks to kbeach who reported this [in a Google Groups post](https://groups.google.com/forum/#!msg/markdown-here/tolrITkqrx0/phElyPBBAhYJ).
* Thanks again to [Emanuele D'Osualdo](https://github.com/bordaigorl) for providing the fix.
Expand Down
19 changes: 17 additions & 2 deletions src/common/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,17 @@ InlineLexer.prototype.output = function(src) {

InlineLexer.prototype.outputLink = function(cap, link) {
if (cap[0].charAt(0) !== '!') {
link.href = link.href.replace(/^(?!#)([^:]+)$/, 'http://$1'); /* adam-p: added to fix MDH issue #57: MD links should automatically add scheme. Note that the presence of a ':' is used to indicate a scheme, so port numbers will defeat this. */

/* adam-p: added to fix MDH issue #57: MD links should automatically add scheme.
Note that the presence of a ':' is used to indicate a scheme, so port
numbers will defeat this. */
link.href = link.href.replace(/^(?!#)([^:]+)$/, 'http://$1');

/* adam-p: added to support automatic anchor links in email. MDH issue #93. */
if (link.href.indexOf('#') === 0) {
link.href = '#' + link.href.slice(1).toLowerCase().replace(/[^\w]+/g, '-');
}

return '<a href="'
+ escape(link.href)
+ '"'
Expand Down Expand Up @@ -855,7 +865,12 @@ Parser.prototype.tok = function() {
return '<hr>\n';
}
case 'heading': {
return '<h'
/* adam-p: added this to make anchors work with email. MDH issue #93. */
var anchor = '<a href="#" name="' +
this.token.text.toLowerCase().replace(/[^\w]+/g, '-') +
'"></a>';

return anchor + '<h'
+ this.token.depth
+ ' id="'
+ this.token.text.toLowerCase().replace(/[^\w]+/g, '-')
Expand Down
22 changes: 22 additions & 0 deletions src/common/test/markdown-render-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ describe('Markdown-Render', function() {
target = '<p><img src="https://chart.googleapis.com/chart?cht=tx&chl=xx" alt="xx"></p>\n';
expect(MarkdownRender.markdownRender(md, userprefs, marked, hljs)).to.equal(target);
});

// Test issue #93: Add support for anchor links: https://github.com/adam-p/markdown-here/issues/57
it('should add anchors to headers', function() {
var md = '# Header Number 1\n\n###### Header Number 6';
var target = '<a href="#" name="header-number-1"></a><h1 id="header-number-1">Header Number 1</h1>\n<a href="#" name="header-number-6"></a><h6 id="header-number-6">Header Number 6</h6>\n';
expect(MarkdownRender.markdownRender(md, userprefs, marked, hljs)).to.equal(target);
});

// Test issue #93: Add support for anchor links: https://github.com/adam-p/markdown-here/issues/57
it('should convert anchor links to point to header auto-anchors', function() {
var md = '[H1](#Header Number 1)\n[H6](#Header Number 6)';
var target = '<p><a href="#header-number-1">H1</a><br><a href="#header-number-6">H6</a></p>\n';
expect(MarkdownRender.markdownRender(md, userprefs, marked, hljs)).to.equal(target);
});

// Test issue #93: Add support for anchor links: https://github.com/adam-p/markdown-here/issues/57
it('should handle non-alphanumeric characters in headers', function() {
var md = '[H1](#a&b!c*d_f)\n# a&b!c*d_f';
var target = '<p><a href="#a-b-c-d_f">H1</a></p>\n<a href="#" name="a-b-c-d_f"></a><h1 id="a-b-c-d_f">a&amp;b!c*d_f</h1>\n';
expect(MarkdownRender.markdownRender(md, userprefs, marked, hljs)).to.equal(target);
});

});


Expand Down

0 comments on commit 83aa9c4

Please sign in to comment.