-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy patharticle-body.ts
More file actions
37 lines (33 loc) · 1.33 KB
/
article-body.ts
File metadata and controls
37 lines (33 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import type { Response } from 'express'
import { Context } from '@/types'
import { ExtendedRequestWithPageInfo } from '../types'
import contextualize from '@/frame/middleware/context/context'
export async function getArticleBody(req: ExtendedRequestWithPageInfo) {
// req.pageinfo is set from pageValidationMiddleware and pathValidationMiddleware
// and is in the ExtendedRequestWithPageInfo
const { page, pathname, archived } = req.pageinfo
if (archived?.isArchived)
throw new Error(`Page ${pathname} is archived and can't be rendered in markdown.`)
// for anything that's not an article (like index pages), don't try to render and
// tell the user what's going on
if (page.documentType !== 'article') {
throw new Error(`Page ${pathname} isn't yet available in markdown.`)
}
// these parts allow us to render the page
const mockedContext: Context = {}
const renderingReq = {
path: pathname,
language: page.languageCode,
pagePath: pathname,
cookies: {},
context: mockedContext,
headers: {
'content-type': 'text/markdown',
},
}
// contextualize and render the page
await contextualize(renderingReq as ExtendedRequestWithPageInfo, {} as Response, () => {})
renderingReq.context.page = page
renderingReq.context.markdownRequested = true
return await page.render(renderingReq.context)
}