|
| 1 | +export function addWidgets(config) { |
| 2 | + config.addPairedShortcode('example', (content, caption) => { |
| 3 | + let rv = "<figure class='box'>\n\n" |
| 4 | + if (caption) rv += `<figcaption class="allcaps" >Example: ${caption}</figcaption>\n\n` |
| 5 | + else rv += `<figcaption class="allcaps">Example</figcaption>\n\n` |
| 6 | + rv += " ~~~ html" |
| 7 | + rv += content |
| 8 | + rv += "~~~\n\n" |
| 9 | + rv += content |
| 10 | + rv += "</figure>" |
| 11 | + return rv |
| 12 | + }) |
| 13 | + |
| 14 | + function syntax(syntax) { |
| 15 | + syntax = syntax |
| 16 | + .replace(/\[\[([\*\+\?])\]\]/g, |
| 17 | + "<sup>$1</sup>" |
| 18 | + ) |
| 19 | + .replace(/\[\[([a-zA-Z0-9 ]+)\]\]/g, |
| 20 | + '<b class="chip syntaxvar"><var>$1</var></b>' |
| 21 | + ) |
| 22 | + |
| 23 | + return `<code class="syntax">${syntax}</code>` |
| 24 | + } |
| 25 | + |
| 26 | + config.addShortcode("syntax", syntax) |
| 27 | + |
| 28 | + function syntaxify(line) { |
| 29 | + return line |
| 30 | + .replace(/`([^`]+)`/g, (match, p1) => syntax(p1)) |
| 31 | + } |
| 32 | + |
| 33 | + config.addPairedShortcode("syntaxes", content => { |
| 34 | + const buf = [] |
| 35 | + const lines = content.split('\n'); |
| 36 | + let dt = false; |
| 37 | + for (const line of lines) { |
| 38 | + if (isJustWhitespace(line)) { |
| 39 | + buf.push(line); |
| 40 | + } else if (!indented(line)) { |
| 41 | + dt = true; |
| 42 | + buf.push('\n<dt>' + syntaxify(line)); |
| 43 | + } else { |
| 44 | + if (dt) buf.push('<dd>\n'); |
| 45 | + dt = false; |
| 46 | + buf.push(dedent(line)); |
| 47 | + } |
| 48 | + } |
| 49 | + return `<div class="box"><dl class="syntaxes">${buf.join('\n')}</dl></div>`; |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +function isJustWhitespace(str) { return /^\s*$/.test(str) } |
| 54 | +function dedent(str) { return str.replace(/^(\t| )/, '') } |
| 55 | +function indented(str) { return str.startsWith(' ') || str.startsWith('\t') } |
0 commit comments