|
| 1 | +#!/usr/bin/env node |
| 2 | +import fs from 'fs' |
| 3 | +import path from 'path' |
| 4 | +import { execSync } from 'child_process' |
| 5 | + |
| 6 | +import { renderLiquid } from '#src/content-render/liquid/index.js' |
| 7 | +import shortVersionsMiddleware from '#src/versions/middleware/short-versions.js' |
| 8 | + |
| 9 | +const { loadPages } = await import('../lib/page-data.js') |
| 10 | +const { allVersions } = await import('#src/versions/lib/all-versions.js') |
| 11 | + |
| 12 | +const contentCopilotDir = path.join(process.cwd(), 'content-copilot') |
| 13 | + |
| 14 | +if (!fs.existsSync(contentCopilotDir)) fs.mkdirSync(contentCopilotDir) |
| 15 | + |
| 16 | +// Load all pages |
| 17 | +const allPages = await loadPages() |
| 18 | +const pages = allPages.filter( |
| 19 | + (page) => |
| 20 | + // Files we want to check: English and FPT only |
| 21 | + // Files we do not want to check: |
| 22 | + // any index.md files |
| 23 | + // /graphql/overview/explorer.md since it's not a real content page |
| 24 | + // /early-access/ since we don't want copilot looking at these |
| 25 | + page.languageCode === 'en' && |
| 26 | + page.applicableVersions.includes('free-pro-team@latest') && |
| 27 | + !page.relativePath.endsWith('index.md') && |
| 28 | + !page.relativePath.endsWith('graphql/overview/explorer.md') && |
| 29 | + !page.relativePath.includes('/early-access/'), |
| 30 | +) |
| 31 | + |
| 32 | +for (const page of pages) { |
| 33 | + console.log(`---\nStart: Creating directories for: ${page.relativePath}`) |
| 34 | + const dirnames = page.relativePath.substring(0, page.relativePath.lastIndexOf('/')) |
| 35 | + |
| 36 | + fs.mkdirSync(`${contentCopilotDir}/${dirnames}`, { recursive: true }, (err) => { |
| 37 | + if (err) throw err |
| 38 | + }) |
| 39 | + // Context needed to render the content liquid |
| 40 | + const req = { language: 'en' } |
| 41 | + const contextualize = (req) => { |
| 42 | + req.context.currentVersionObj = req.context.allVersions[req.context.currentVersion] |
| 43 | + shortVersionsMiddleware(req, null, () => {}) |
| 44 | + } |
| 45 | + |
| 46 | + req.context = { |
| 47 | + currentLanguage: 'en', |
| 48 | + currentVersion: 'free-pro-team@latest', |
| 49 | + page: {}, |
| 50 | + allVersions, |
| 51 | + } |
| 52 | + contextualize(req) |
| 53 | + |
| 54 | + try { |
| 55 | + console.log(`Rendering markdown for: ${page.title}`) |
| 56 | + let frontmatterMarkdown = `# ${await renderLiquid(page.title, req.context)}\n\n` |
| 57 | + |
| 58 | + if (page.intro) { |
| 59 | + frontmatterMarkdown += `${await renderLiquid(page.intro, req.context)}\n\n` |
| 60 | + } |
| 61 | + |
| 62 | + if (page.permissions) { |
| 63 | + frontmatterMarkdown += `**Who can use this feature**: ${await renderLiquid( |
| 64 | + page.permissions, |
| 65 | + req.context, |
| 66 | + )}\n` |
| 67 | + } |
| 68 | + |
| 69 | + if (page.product) { |
| 70 | + frontmatterMarkdown += `${await renderLiquid(page.product, req.context)}\n` |
| 71 | + } |
| 72 | + const rendered = await renderLiquid(page.markdown, req.context) |
| 73 | + console.log('Rendered markdown') |
| 74 | + console.log(`Writing file to: ${contentCopilotDir}/${page.relativePath}`) |
| 75 | + fs.writeFileSync( |
| 76 | + `${contentCopilotDir}/${page.relativePath}`, |
| 77 | + frontmatterMarkdown + rendered, |
| 78 | + 'utf8', |
| 79 | + ) |
| 80 | + console.log(`Done: written file\n---`) |
| 81 | + } catch (err) { |
| 82 | + console.log(err) |
| 83 | + } |
| 84 | +} |
| 85 | +console.log('---\nWriting files done. Now linting content...\n') |
| 86 | +// Content linter to remove any blank lines |
| 87 | +execSync('npm run lint-content -- --paths content-copilot --rules no-multiple-blanks --fix') |
| 88 | +console.log(`Finished - content is available in: ${contentCopilotDir}`) |
0 commit comments