'use strict'; module.exports = function footnote_block(state) { var i, l, t, list, tokens, current, currentLabel, anchor, level = 0, insideRef = false, refTokens = Object.create(null); if (!state.env.footnotes) { return; } state.tokens = state.tokens.filter(function(tok) { if (tok.type === 'footnote_reference_open') { insideRef = true; current = []; currentLabel = tok.label; return false; } if (tok.type === 'footnote_reference_close') { insideRef = false; refTokens[currentLabel] = current; return false; } if (insideRef) { current.push(tok); } return !insideRef; }); if (!state.env.footnotes.list) { return; } list = state.env.footnotes.list; state.tokens.push({ type: 'footnote_block_open', level: level++ }); for (i = 0, l = list.length; i < l; i++) { state.tokens.push({ type: 'footnote_open', id: i, level: level++ }); if (list[i].tokens) { tokens = []; tokens.push({ type: 'paragraph_open', tight: false, level: level++ }); tokens.push({ type: 'inline', content: '', level: level, children: list[i].tokens }); tokens.push({ type: 'paragraph_close', tight: false, level: --level }); } else if (list[i].label) { tokens = refTokens[list[i].label]; } anchor = { type: 'footnote_anchor', id: i, level: level }; state.tokens = state.tokens.concat(tokens); if (state.tokens[state.tokens.length - 1].type === 'paragraph_close') { t = state.tokens.pop(); state.tokens.push(anchor); state.tokens.push(t); } else { state.tokens.push(anchor); } state.tokens.push({ type: 'footnote_close', level: --level }); } state.tokens.push({ type: 'footnote_block_close', level: --level }); };