Skip to content

Commit

Permalink
Fixed #155: Make header auto-anchors optional. While doing that, also…
Browse files Browse the repository at this point in the history
… updated marked.js to markedjs/marked@613bf6a. Need updated translations for new text.
  • Loading branch information
adam-p committed Jan 28, 2014
1 parent 08436e4 commit ab41f75
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 167 deletions.
10 changes: 10 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -333,5 +333,15 @@
"new_changelist_items": {
"message": "NEW",
"description": "Used to highlight the part of the changelist that is new"
},
"options_page__header_anchors_enabled_label": {
"message": "<b>Enable automatic header anchors.</b>",
"description": "Label for the checkbox that enables the automatic header anchors feature. This means that an 'anchor' is created at every heading (H1, H2, etc.) in the user's Markdown. The user can then create links that will jump to these anchors, like in the table of contents of a document."
},
"options_page__header_anchors_enabled_1": {
"message": "This is great if you want to put a table of contents or other internal links into your content. <a href=\"https://github.com/adam-p/markdown-here/wiki/Tips-and-Tricks#wiki-header-anchors\" target=\"_blank\">Learn about how to use it in the MDH wiki.</a>"
},
"options_page__header_anchors_enabled_2": {
"message": "It's disabled by default because it <a href=\"https://github.com/adam-p/markdown-here/issues/155\" target=\"_blank\">creates visual noise in Thunderbird</a>. (And most people probably won't use the feature.)"
}
}
41 changes: 41 additions & 0 deletions src/common/markdown-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,48 @@ function markdownRender(mdText, userprefs, marked, hljs) {
.replace(/\{urlmathcode\}/ig, encodeURIComponent(mathcode));
}

// Hook into some of Marked's renderer customizations
var markedRenderer = new marked.Renderer();

var sanitizeLinkForAnchor = function(text) {
return text.toLowerCase().replace(/[^\w]+/g, '-');
};

var defaultHeadingRenderer = markedRenderer.heading;
markedRenderer.heading = function (text, level, raw) {
if (userprefs['header-anchors-enabled']) {
// Add an anchor right above the heading. See MDH issue #93.
var sanitizedText = sanitizeLinkForAnchor(text);
var anchorLink = '<a href="#" name="' + sanitizedText + '"></a>';
return '<h' + level + '>' +
anchorLink +
text +
'</h' + level + '>\n';
}
else {
return defaultHeadingRenderer.call(this, text, level, raw);
}
};

var defaultLinkRenderer = markedRenderer.link;
markedRenderer.link = function(href, title, text) {
// 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.
href = href.replace(/^(?!#)([^:]+)$/, 'http://$1');

if (userprefs['header-anchors-enabled']) {
// Add an anchor right above the heading. See MDH issue #93.
if (href.indexOf('#') === 0) {
href = '#' + sanitizeLinkForAnchor(href.slice(1).toLowerCase());
}
}

return defaultLinkRenderer.call(this, href, title, text);
};

var markedOptions = {
renderer: markedRenderer,
gfm: true,
pedantic: false,
sanitize: false,
Expand Down
Loading

0 comments on commit ab41f75

Please sign in to comment.