Skip to content

Commit

Permalink
add footnotes. see #351.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Feb 23, 2014
1 parent bcf206e commit abe5262
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var block = {
blockquote: /^( *>[^\n]+(\n(?!def)[^\n]+)*\n*)+/,
list: /^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
html: /^ *(?:comment|closed|closing) *(?:\n{2,}|\s*$)/,
footnote: /^ *\[\^([^\]]+)\]: /,
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/,
table: noop,
paragraph: /^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,
Expand Down Expand Up @@ -159,6 +160,16 @@ Lexer.prototype.token = function(src, top, bq) {
, l;

while (src) {
if (top && this.footnote
&& this.tokens[this.tokens.length-1].type !== 'footnote_start'
&& this.tokens[this.tokens.length-1].type !== 'parapgraph'
&& this.tokens[this.tokens.length-1].type !== 'newline') {
this.footenote = false;
this.tokens.push({
type: 'footnote_end'
});
}

// newline
if (cap = this.rules.newline.exec(src)) {
src = src.substring(cap[0].length);
Expand Down Expand Up @@ -365,6 +376,17 @@ Lexer.prototype.token = function(src, top, bq) {
continue;
}

// footnote
if ((!bq && top) && (cap = this.rules.footnote.exec(src))) {
src = src.substring(cap[0].length);
this.footnote = true;
this.tokens.push({
type: 'footnote_start',
text: cap[1]
});
continue;
}

// def
if ((!bq && top) && (cap = this.rules.def.exec(src))) {
src = src.substring(cap[0].length);
Expand Down Expand Up @@ -438,6 +460,13 @@ Lexer.prototype.token = function(src, top, bq) {
}
}

if (top && this.footnote) {
this.footenote = false;
this.tokens.push({
type: 'footnote_end'
});
}

return this.tokens;
};

Expand All @@ -451,6 +480,7 @@ var inline = {
url: noop,
tag: /^<!--[\s\S]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^'">])*?>/,
link: /^!?\[(inside)\]\(href\)/,
footnoteref: /^ *\[\^([^\]]+)\]/,
reflink: /^!?\[(inside)\]\s*\[([^\]]*)\]/,
nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,
Expand Down Expand Up @@ -623,6 +653,13 @@ InlineLexer.prototype.output = function(src) {
continue;
}

// footnote
if (cap = this.rules.footnoteref.exec(src)) {
src = src.substring(cap[0].length);
out += this.renderer.footnoteref(escape(cap[1], true));
continue;
}

// reflink, nolink
if ((cap = this.rules.reflink.exec(src))
|| (cap = this.rules.nolink.exec(src))) {
Expand Down Expand Up @@ -859,6 +896,39 @@ Renderer.prototype.del = function(text) {
return '<del>' + text + '</del>';
};

Renderer.prototype.footnoteref = function(ref) {
return '<sup id="fnref' + ref + '">'
+ '<a href="#fn' + ref + '">' + ref + '</a>'
+ '</sup>';
};

Renderer.prototype.footnote = function(ref, body) {
return '<li id="fn' + ref + '">' + body
+ '<a href="#fnref' + ref + '" class="footnoteBackLink" '
+ 'title="Jump back to footnote ' + ref
+ ' in the text.">↩</a>'

This comment has been minimized.

Copy link
@totocaster

totocaster Jul 31, 2014

Would be better to replace ↩ to safer &#8617;

+ '</li>\n';
};

Renderer.prototype.footnotes = function(footnotes) {
var text = ''
, items = ''
, i = 0;

for (; i < footnotes.length; i++) {
items += this.footnote(footnotes[i].ref, footnotes[i].body);
}

text += this.hr();
text += '<div class="footnotes">\n';
text += this.list(items, true);
text += '</div>\n';

footnotes.length = 0;

return text;
};

Renderer.prototype.link = function(href, title, text) {
if (this.options.sanitize) {
try {
Expand Down Expand Up @@ -900,6 +970,7 @@ function Parser(options) {
this.options.renderer = this.options.renderer || new Renderer;
this.renderer = this.options.renderer;
this.renderer.options = this.options;
this.footnotes = [];
}

/**
Expand All @@ -924,6 +995,10 @@ Parser.prototype.parse = function(src) {
out += this.tok();
}

if (this.footnotes.length) {
out += this.renderer.footnotes(this.footnotes);
}

return out;
};

Expand Down Expand Up @@ -1024,6 +1099,21 @@ Parser.prototype.tok = function() {

return this.renderer.blockquote(body);
}
case 'footnote_start': {
var body = ''
, ref = escape(this.token.text, true);

while (this.next().type !== 'footnote_end') {
body += this.tok();
}

this.footnotes.push({
ref: ref,
body: body
});

return '';
}
case 'list_start': {
var body = ''
, ordered = this.token.ordered;
Expand Down

0 comments on commit abe5262

Please sign in to comment.