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

Feature footnotes #351

Closed
wants to merge 4 commits into from
Closed
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
122 changes: 104 additions & 18 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var block = {
blockquote: /^( *>[^\n]+(\n[^\n]+)*\n*)+/,
list: /^( *)(bull) [\s\S]+?(?:hr|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
html: /^ *(?:comment|closed|closing) *(?:\n{2,}|\s*$)/,
def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/,
def: /^ *\[([^^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/,
footnote: noop,
table: noop,
paragraph: /^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,
text: /^[^\n]+/
Expand Down Expand Up @@ -73,21 +74,39 @@ block.gfm = merge({}, block.normal, {
fences: /^ *(`{3,}|~{3,}) *(\S+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/,
paragraph: /^/
});

block.gfm.paragraph = replace(block.paragraph)
('(?!', '(?!'
+ block.gfm.fences.source.replace('\\1', '\\2') + '|'
+ block.list.source.replace('\\1', '\\3') + '|')
();

/**
* GFM + Tables Block Grammar
* Tables Block Grammar
*/

block.tables = merge({}, block.gfm, {
block.tables = {
nptable: /^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/,
table: /^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/
});
};

/**
* Footnotes Block Grammar
*/
block.footnotes = {
footnote: /^\[\^([^\]]+)\]: *([^\n]*(?:\n+|$)(?: {1,}[^\n]*(?:\n+|$))*)/
};
block.footnotes.normal = {
footnote: block.footnotes.footnote
};
block.footnotes.normal.paragraph = replace(block.paragraph)(
'))+)', '|' + block.footnotes.footnote.source + '))+)'
)();
block.footnotes.gfm = {
footnote: block.footnotes.footnote
};
block.footnotes.gfm.paragraph = replace(block.gfm.paragraph)(
'))+)', '|' + block.footnotes.footnote.source + '))+)'
)();

/**
* Block Lexer
Expand All @@ -96,15 +115,21 @@ block.tables = merge({}, block.gfm, {
function Lexer(options) {
this.tokens = [];
this.tokens.links = {};
this.tokens.footnotes = [];
this.options = options || marked.defaults;
this.rules = block.normal;

if (this.options.gfm) {
if (this.options.tables) {
this.rules = block.tables;
} else {
this.rules = block.gfm;
this.rules = block.gfm;
if (this.options.footnotes) {
this.rules = merge({}, this.rules, block.footnotes.gfm);
}
} else if (this.options.footnotes) {
this.rules = merge({}, this.rules, block.footnotes.normal);
}

if (this.options.tables) {
this.rules = merge({}, this.rules, block.tables);
}
}

Expand Down Expand Up @@ -144,6 +169,7 @@ Lexer.prototype.lex = function(src) {
Lexer.prototype.token = function(src, top) {
var src = src.replace(/^ +$/gm, '')
, next
, key
, loose
, cap
, bull
Expand Down Expand Up @@ -370,6 +396,15 @@ Lexer.prototype.token = function(src, top) {
continue;
}

// footnote
if (top && (cap = this.rules.footnote.exec(src))) {
src = src.substring(cap[0].length);
key = cap[1].slice(1).toLowerCase();
this.tokens.footnotes.push({key: key, text: cap[2]});
this.tokens.footnotes[key] = {text: cap[2], count: this.tokens.footnotes.length};
continue;
}

// table (gfm)
if (top && (cap = this.rules.table.exec(src))) {
src = src.substring(cap[0].length);
Expand Down Expand Up @@ -453,10 +488,11 @@ var inline = {
code: /^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/,
br: /^ {2,}\n(?!\s*$)/,
del: noop,
footnote: noop,
text: /^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)/
};

inline._inside = /(?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/;
inline._inside = /(?:\[[^^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/;
inline._href = /\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/;

inline.link = replace(inline.link)
Expand All @@ -478,10 +514,10 @@ inline.normal = merge({}, inline);
* Pedantic Inline Grammar
*/

inline.pedantic = merge({}, inline.normal, {
inline.pedantic = {
strong: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
em: /^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/
});
};

/**
* GFM Inline Grammar
Expand All @@ -506,13 +542,22 @@ inline.breaks = merge({}, inline.gfm, {
text: replace(inline.gfm.text)('{2,}', '*')()
});

/**
* Footnote Inline Grammar
*/

inline.footnote = {
footnote: /^\[\^([^\]]+)\]/
};

/**
* Inline Lexer & Compiler
*/

function InlineLexer(links, options) {
function InlineLexer(links, footnotes, options) {
this.options = options || marked.defaults;
this.links = links;
this.footnotes = footnotes || {};
this.rules = inline.normal;
this.renderer = this.options.renderer || new Renderer;
this.renderer.options = this.options;
Expand All @@ -528,8 +573,14 @@ function InlineLexer(links, options) {
} else {
this.rules = inline.gfm;
}
} else if (this.options.pedantic) {
this.rules = inline.pedantic;
}

if (this.options.footnotes) {
this.rules = merge({}, this.rules, inline.footnote);
}

if (this.options.pedantic) {
this.rules = merge({}, this.rules, inline.pedantic);
}
}

Expand All @@ -543,8 +594,8 @@ InlineLexer.rules = inline;
* Static Lexing/Compiling Method
*/

InlineLexer.output = function(src, links, options) {
var inline = new InlineLexer(links, options);
InlineLexer.output = function(src, links, footnotes, options) {
var inline = new InlineLexer(links, footnotes, options);
return inline.output(src);
};

Expand All @@ -555,6 +606,8 @@ InlineLexer.output = function(src, links, options) {
InlineLexer.prototype.output = function(src) {
var out = ''
, link
, key
, ret
, text
, href
, cap;
Expand Down Expand Up @@ -601,6 +654,16 @@ InlineLexer.prototype.output = function(src) {
continue;
}

// footnote
if (cap = this.rules.footnote.exec(src)) {
key = cap[1].toLowerCase();
if (ret = this.footnotes[key]) {
src = src.substring(cap[0].length);
out += this.renderer.footnoteref(key, ret.count);
continue;
}
}

// link
if (cap = this.rules.link.exec(src)) {
src = src.substring(cap[0].length);
Expand Down Expand Up @@ -875,6 +938,24 @@ Renderer.prototype.image = function(href, title, text) {
return out;
};

Renderer.prototype.footnoteref = function(key, count) {
var out = '<sup class="footnote-ref" id="fnref-' + escape(key) + '">';
out += '<a href="#fn-' + escape(key) + '">' + count + '</a></sup>';
return out;
};

Renderer.prototype.footnotes = function(notes) {
var out = '<ol class="footnotes">';
for (var i = 0; i < notes.length; i++) {
out += '<li id="fn-' + escape(notes[i].key) + '">';
out += notes[i].text;
out += '<a href="#fnref-' + escape(notes[i].key) + '">&#8617;</a>'
out += '</li>';
}
out += '</ol>';
return out;
};

/**
* Parsing & Compiling
*/
Expand Down Expand Up @@ -902,14 +983,18 @@ Parser.parse = function(src, options, renderer) {
*/

Parser.prototype.parse = function(src) {
this.inline = new InlineLexer(src.links, this.options, this.renderer);
this.inline = new InlineLexer(src.links, src.footnotes, this.options);
this.tokens = src.reverse();

var out = '';
while (this.next()) {
out += this.tok();
}

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

return out;
};

Expand Down Expand Up @@ -1209,6 +1294,7 @@ marked.setOptions = function(opt) {
marked.defaults = {
gfm: true,
tables: true,
footnotes: false,
breaks: false,
pedantic: false,
sanitize: false,
Expand Down