Skip to content

Commit ef98b86

Browse files
committed
docs: [site] add robots.txt generation to postbuild tasks
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
1 parent 2936463 commit ef98b86

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

docs/.vitepress/config.ts

+29-13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { globby } from 'globby'
1212
import fs from 'node:fs/promises'
1313
import path from 'node:path'
1414
import { fileURLToPath } from 'node:url'
15+
import pupa from 'pupa'
1516
import { SitemapStream, streamToPromise } from 'sitemap'
1617
import tsconfigpaths from 'vite-tsconfig-paths'
1718
import {
@@ -59,32 +60,34 @@ const index: string = pkg.name.replace(/.+\//, '')
5960
const config: UserConfig = defineConfig({
6061
appearance: 'dark',
6162
/**
62-
* Performs post-build tasks.
63+
* Performs postbuild tasks.
6364
*
6465
* This includes:
6566
*
66-
* - Building and writing `sitemap.xml`
67+
* 1. Writing `sitemap.xml` to `config.outDir`
68+
* 2. Writing `robots.txt` to `config.outDir`
6769
*
6870
* @async
6971
*
7072
* @param {SiteConfig} config - Site configuration
7173
* @param {string} config.outDir - Absolute path to output directory
74+
* @param {string} config.root - Absolute path to project directory
7275
* @return {Promise<void>} Nothing when complete
7376
*/
74-
async buildEnd({ outDir }: SiteConfig): Promise<void> {
77+
async buildEnd({ outDir, root }: SiteConfig): Promise<void> {
7578
/**
76-
* Sitemap stream.
79+
* Sitemap routes.
7780
*
78-
* @var {SitemapStream} stream
81+
* @const {[string, CheerioAPI][]} routes
7982
*/
80-
let stream: SitemapStream = new SitemapStream({ hostname: HOSTNAME })
83+
const routes: [string, CheerioAPI][] = []
8184

8285
/**
83-
* Sitemap routes.
86+
* Sitemap stream.
8487
*
85-
* @const {[string, CheerioAPI][]} routes
88+
* @const {SitemapStream} stream
8689
*/
87-
const routes: [string, CheerioAPI][] = []
90+
const stream: SitemapStream = new SitemapStream({ hostname: HOSTNAME })
8891

8992
// get sitemap routes
9093
for (const route of await globby('**.html', { cwd: outDir })) {
@@ -105,11 +108,24 @@ const config: UserConfig = defineConfig({
105108
})
106109
}
107110

108-
// end stream
109-
stream = stream.end()
110-
111111
// write sitemap.xml
112-
await fs.writeFile(`${outDir}/sitemap.xml`, await streamToPromise(stream))
112+
await fs.writeFile(
113+
path.resolve(outDir, 'sitemap.xml'),
114+
await streamToPromise(stream.end())
115+
)
116+
117+
/**
118+
* `robots.txt` template file path.
119+
*
120+
* @const {string} robots
121+
*/
122+
const robots: string = path.resolve(root, '.vitepress/templates/robots.txt')
123+
124+
// write robots.txt
125+
await fs.writeFile(
126+
path.resolve(outDir, 'robots.txt'),
127+
pupa(await fs.readFile(robots, 'utf8'), { HOSTNAME })
128+
)
113129

114130
return void 0
115131
},

docs/.vitepress/templates/robots.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
User-agent: *
2+
Disallow: /404
3+
Allow: /
4+
Sitemap: {HOSTNAME}/sitemap.xml

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
"prettier": "2.7.1",
143143
"prettier-plugin-sh": "0.12.8",
144144
"pretty-format": "29.2.1",
145+
"pupa": "3.1.0",
145146
"serve": "14.0.1",
146147
"sitemap": "7.1.1",
147148
"trash-cli": "5.0.0",

yarn.lock

+17
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,7 @@ __metadata:
11351135
prettier: "npm:2.7.1"
11361136
prettier-plugin-sh: "npm:0.12.8"
11371137
pretty-format: "npm:29.2.1"
1138+
pupa: "npm:3.1.0"
11381139
read-pkg-up: "npm:9.1.0"
11391140
serve: "npm:14.0.1"
11401141
sitemap: "npm:7.1.1"
@@ -4822,6 +4823,13 @@ __metadata:
48224823
languageName: node
48234824
linkType: hard
48244825

4826+
"escape-goat@npm:^4.0.0":
4827+
version: 4.0.0
4828+
resolution: "escape-goat@npm:4.0.0"
4829+
checksum: 5878a265bf82e3c74fc87a1ca57fc7ad4b51c3f6d2d8a0f6dc18f43c003546d0b42999b5cf7edfb72352df1bd94676cec9cfda2b94828df96a8046123725eed0
4830+
languageName: node
4831+
linkType: hard
4832+
48254833
"escape-string-regexp@npm:5.0.0":
48264834
version: 5.0.0
48274835
resolution: "escape-string-regexp@npm:5.0.0"
@@ -8257,6 +8265,15 @@ __metadata:
82578265
languageName: node
82588266
linkType: hard
82598267

8268+
"pupa@npm:3.1.0":
8269+
version: 3.1.0
8270+
resolution: "pupa@npm:3.1.0"
8271+
dependencies:
8272+
escape-goat: "npm:^4.0.0"
8273+
checksum: 948dd9a0a7587374a5aff7b773a41ad5c5173a295de4e060064394f9f36dee97a53a76f5550b6bc8ba82f78951f43fd6238e671cbea36ee8d1ec48b86ffe272d
8274+
languageName: node
8275+
linkType: hard
8276+
82608277
"pupa@npm:^2.1.1":
82618278
version: 2.1.1
82628279
resolution: "pupa@npm:2.1.1"

0 commit comments

Comments
 (0)