-
Notifications
You must be signed in to change notification settings - Fork 0
/
basedOnOutput.ts
184 lines (145 loc) · 4.8 KB
/
basedOnOutput.ts
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { readFile } from 'node:fs/promises'
import { dirname, join, relative } from 'node:path'
import * as cheerio from 'cheerio'
import Fuse from 'fuse.js'
import { convert } from 'html-to-text'
import { isEmpty, map, uniq } from 'lodash-es'
import { OutputBaseAstroFuseConfig, OutputBaseSearchable } from './types'
import { log } from './util'
import type { AstroConfig, AstroIntegration } from 'astro'
import type { Plugin } from 'vite'
import { OUTFILE, PLUGIN_NAME } from './index'
export function basedOnOutput(config: AstroConfig): Plugin {
const outDir = config.outDir.pathname
const outputPath = join(outDir, OUTFILE)
return {
name: PLUGIN_NAME,
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
if (req.url?.endsWith(`/${OUTFILE}`)) {
const file = await readFile(outputPath)
return res.setHeader('Content-Type', 'application/json').end(file)
}
return next()
})
},
}
}
type AstroBuildDoneEvent = Parameters<
NonNullable<AstroIntegration['hooks']['astro:build:done']>
>[0]
export function writeFuseIndex(
config: AstroConfig,
_config: OutputBaseAstroFuseConfig,
opts: AstroBuildDoneEvent
) {
const { routes, pages } = opts
let finalSiteBasePath: string
if (config.site) {
finalSiteBasePath = new URL(config.base, config.site).pathname
} else {
console.warn(
'The astro-fuse integration requires the `site` astro.config option when source mode. Skipping.'
)
return
}
let pagePaths = map(pages, ({ pathname }) => {
if (pathname !== '' && !finalSiteBasePath.endsWith('/')) {
finalSiteBasePath += '/'
}
return finalSiteBasePath + pathname
})
const routePaths = routes.reduce<string[]>((urls, r) => {
if (r.type !== 'page') {
return urls
}
/**
* Dynamic URLs have entries with `undefined` pathnames
*/
if (r.pathname) {
if (isStatusCodePage(r.pathname ?? r.route)) {
return urls
}
/**
* remove the initial slash from relative pathname
* because `finalSiteUrl` always has trailing slash
*/
const path = finalSiteBasePath + r.generate(r.pathname).substring(1)
if (config.trailingSlash === 'never') {
urls.push(path)
} else if (config.build.format === 'directory' && !path.endsWith('/')) {
urls.push(`${path}/`)
} else {
urls.push(path)
}
}
return urls
}, [])
pagePaths = Array.from(new Set([...pagePaths, ...routePaths]))
try {
if (_config.filter) {
pagePaths = pagePaths.filter(_config.filter)
}
} catch (err) {
log(`Error filtering pages\n${String(err)}`, 'error')
return
}
if (pagePaths.length === 0) {
log(`No pages found!\n\`${OUTFILE}\` not created.`, 'warn')
return
}
const outDir = config.outDir.pathname
const outputPath = join(outDir, OUTFILE)
if (!existsSync(outDir)) {
mkdirSync(dirname(outputPath))
}
const list: OutputBaseSearchable[] = []
for (const path of pagePaths) {
const filePath = path.endsWith('/') ? `${path}index.html` : `${path}.html`
let content = ''
try {
content = readFileSync(join(config.outDir.pathname, filePath), {
encoding: 'utf8',
}).toString()
} catch (err) {
log(`Skipped!\nNo output found! ${filePath}`, 'warn')
continue
}
const $ = cheerio.load(content)
if (_config.extractContentFromHTML) {
if (typeof _config.extractContentFromHTML === 'string') {
content = $(_config.extractContentFromHTML).html() ?? ''
} else {
content = _config.extractContentFromHTML($).html() ?? ''
}
}
if (isEmpty(content)) {
log(`Skipped!\nNo content! ${filePath}`, 'warn')
continue
}
let frontmatter: Record<string, string> = {}
if (_config.extractFrontmatterFromHTML) {
frontmatter = _config.extractFrontmatterFromHTML($, path)
} else {
let title = $('meta[property="og:title"]').first().text()
if (!title) {
title = $('h1').first().text()
}
frontmatter = { title }
}
list.push({ pathname: path, content: convert(content), frontmatter })
}
const _keys = uniq(['content', ...(_config?.keys ?? [])])
const index = Fuse.createIndex(_keys, list, _config?.getFn ? { getFn: _config.getFn } : undefined)
writeFileSync(outputPath, JSON.stringify({ list, index: index.toJSON() }))
log(`\`${OUTFILE}\` created at \`${relative(process.cwd(), dirname(outputPath))}\``)
}
const STATUS_CODE_PAGES = new Set(['404', '500'])
function isStatusCodePage(pathname: string): boolean {
if (pathname.endsWith('/')) {
pathname = pathname.slice(0, -1)
}
const end = pathname.split('/').pop() ?? ''
return STATUS_CODE_PAGES.has(end)
}