|
| 1 | +const fs = require('fs') |
| 2 | +const path = require('path') |
| 3 | + |
| 4 | +const parseMd = content => { |
| 5 | + const sectionMap = {} |
| 6 | + let title = '', |
| 7 | + synonyms = [], |
| 8 | + currentSection = null, |
| 9 | + currentLines = [] |
| 10 | + const flush = () => { |
| 11 | + if (currentSection) sectionMap[currentSection] = currentLines.join('\n').trim() |
| 12 | + } |
| 13 | + content |
| 14 | + .trim() |
| 15 | + .split('\n') |
| 16 | + .forEach(line => { |
| 17 | + if (line.startsWith('# ')) title = line.slice(2).trim() |
| 18 | + else if (line.startsWith('## ')) { |
| 19 | + flush() |
| 20 | + currentSection = line.slice(3).trim() |
| 21 | + currentLines = [] |
| 22 | + } else if (!currentSection && title && line.trim()) |
| 23 | + synonyms = line |
| 24 | + .split(',') |
| 25 | + .map(s => s.trim()) |
| 26 | + .filter(Boolean) |
| 27 | + else if (currentSection) currentLines.push(line) |
| 28 | + }) |
| 29 | + flush() |
| 30 | + return { title, synonyms, sectionMap } |
| 31 | +} |
| 32 | + |
| 33 | +const renderWord = (slug, { title, synonyms, sectionMap }) => { |
| 34 | + let html = `<section id="${slug}" class="word-section">\n <h2>${title}</h2>\n` |
| 35 | + if (synonyms.length) |
| 36 | + html += ` <ul class="synonyms">\n${synonyms.map(s => ` <li class="know">${s}</li>`).join('\n')}\n </ul>\n` |
| 37 | + |
| 38 | + const renderSection = (name, body) => { |
| 39 | + let inner |
| 40 | + if (name === 'Demo') { |
| 41 | + inner = ` <div class="demo-placeholder"><p>Demo coming soon</p></div>` |
| 42 | + } else if (name === 'Examples') { |
| 43 | + inner = ` <ul class="examples-list">\n${body |
| 44 | + .split('\n') |
| 45 | + .filter(l => l.startsWith('- ')) |
| 46 | + .map(l => ` <li>${l.slice(2)}</li>`) |
| 47 | + .join('\n')}\n </ul>` |
| 48 | + } else if (body.toLowerCase().trim() === 'coming soon.') { |
| 49 | + inner = ` <p class="coming-soon">Coming soon.</p>` |
| 50 | + } else { |
| 51 | + inner = body |
| 52 | + .split('\n\n') |
| 53 | + .filter(p => p.trim()) |
| 54 | + .map(p => ` <p>${p.trim()}</p>`) |
| 55 | + .join('\n') |
| 56 | + } |
| 57 | + return ` <div class="entry-section">\n <h3>${name}</h3>\n${inner}\n </div>` |
| 58 | + } |
| 59 | + |
| 60 | + ;['Definition', 'Examples', 'Demo', "Claude's Commentary", "Matt's Commentary"].forEach(name => { |
| 61 | + if (sectionMap[name] !== undefined) html += renderSection(name, sectionMap[name]) + '\n' |
| 62 | + }) |
| 63 | + return html + '</section>' |
| 64 | +} |
| 65 | + |
| 66 | +module.exports = ({ content, pageDir }) => { |
| 67 | + if (!content.includes('{word-sections}') && !content.includes('{sidebar}')) return {} |
| 68 | + const meta = JSON.parse(fs.readFileSync(path.join(pageDir, 'tree.json'), 'utf8')) |
| 69 | + |
| 70 | + const wordSections = meta |
| 71 | + .map( |
| 72 | + ({ label, slugs }) => |
| 73 | + `<h2 class="group-label">${label}</h2>\n` + |
| 74 | + slugs |
| 75 | + .map(slug => { |
| 76 | + const mdPath = path.join(pageDir, `${slug}.md`) |
| 77 | + return fs.existsSync(mdPath) ? renderWord(slug, parseMd(fs.readFileSync(mdPath, 'utf8'))) : '' |
| 78 | + }) |
| 79 | + .join('\n'), |
| 80 | + ) |
| 81 | + .join('\n') |
| 82 | + |
| 83 | + const sidebar = meta |
| 84 | + .map( |
| 85 | + ({ label, slugs }) => |
| 86 | + `<div class="nav-group">\n <div class="nav-label">${label}</div>\n` + |
| 87 | + slugs |
| 88 | + .map(slug => { |
| 89 | + const mdPath = path.join(pageDir, `${slug}.md`) |
| 90 | + if (!fs.existsSync(mdPath)) return '' |
| 91 | + const { title } = parseMd(fs.readFileSync(mdPath, 'utf8')) |
| 92 | + return ` <a href="#${slug}">${title}</a>` |
| 93 | + }) |
| 94 | + .join('\n') + |
| 95 | + '\n</div>', |
| 96 | + ) |
| 97 | + .join('\n') |
| 98 | + |
| 99 | + return { 'word-sections': wordSections, sidebar } |
| 100 | +} |
0 commit comments