Skip to content
Merged
Show file tree
Hide file tree
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
45 changes: 34 additions & 11 deletions src/dev-toc/generate.js → src/dev-toc/generate.ts
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ import fs from 'fs'
import path from 'path'
import { execSync } from 'child_process'
import { program } from 'commander'
import type { NextFunction, Response } from 'express'
import type { ExtendedRequest } from '@/types'
import fpt from '#src/versions/lib/non-enterprise-default-version.js'
import { allVersionKeys } from '#src/versions/lib/all-versions.js'
import { liquid } from '#src/content-render/index.js'
import contextualize from '#src/frame/middleware/context/context'
import contextualize from '#src/frame/middleware/context/context.js'

interface CommandOptions {
openSections?: string | string[]
}

const layoutFilename = path.posix.join(process.cwd(), 'src/dev-toc/layout.html')
const layout = fs.readFileSync(layoutFilename, 'utf8')
Expand All @@ -22,19 +28,30 @@ program
)
.parse(process.argv)

const options = program.opts()
const options = program.opts<CommandOptions>()

const openSections = options.openSections || ''
const defaultOpenSections = Array.isArray(openSections) ? openSections : [openSections]

main()

async function main() {
const next = () => {}
const res = {}
const req = { language: 'en', cookies: {} }

async function recurse(tree) {
async function main(): Promise<void> {
const next = (() => {}) as NextFunction
const res = {} as Response
const req = {
language: 'en',
cookies: {},
headers: {},
query: {},
path: '',
method: 'GET',
get: () => '',
header: () => '',
accepts: () => false,
context: {} as any,
} as unknown as ExtendedRequest

async function recurse(tree: any): Promise<void> {
const { page } = tree
tree.renderedFullTitle = page.rawTitle.includes('{')
? await liquid.parseAndRender(page.rawTitle, req.context)
Expand All @@ -58,12 +75,18 @@ async function main() {
await contextualize(req, res, next)

// Add the tree to the req.context.
req.context.currentEnglishTree = req.context.siteTree.en[req.context.currentVersion]
if (req.context && req.context.siteTree && req.context.currentVersion) {
req.context.currentEnglishTree = req.context.siteTree.en[req.context.currentVersion]
}

await recurse(req.context.currentEnglishTree)
if (req.context && req.context.currentEnglishTree) {
await recurse(req.context.currentEnglishTree)
}

// Add any defaultOpenSections to the context.
req.context.defaultOpenSections = defaultOpenSections
if (req.context) {
req.context.defaultOpenSections = defaultOpenSections
}

// Parse the layout in src/dev-toc/layout.html with the context we created above.
const outputHtml = await liquid.parseAndRender(layout, Object.assign({}, req.context))
Expand Down
6 changes: 3 additions & 3 deletions src/dev-toc/index.js → src/dev-toc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ document.addEventListener('DOMContentLoaded', async () => {
devToc()
})

function devToc() {
const expandButton = document.querySelector('.js-expand')
function devToc(): void {
const expandButton = document.querySelector('.js-expand') as HTMLButtonElement | null
if (!expandButton) return

const detailsElements = document.querySelectorAll('details')
const detailsElements = document.querySelectorAll('details') as NodeListOf<HTMLDetailsElement>

expandButton.addEventListener('click', () => {
// on click, toggle all the details elements open or closed
Expand Down
Loading