Skip to content

Commit

Permalink
feat: add sitemap and update workflows (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
rwv authored Jan 2, 2025
1 parent 45a2823 commit be52597
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ jobs:
- name: Build PDFs
run: pnpm run build:pdfs

- name: Build Sitemap
run: pnpm run build:sitemap

- name: Run build
run: pnpm run build

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ coverage
*.tsbuildinfo

public/pdfs/
public/sitemap.xml
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"format": "prettier --write src/",
"format-check": "prettier --check src/",
"build:pdfs": "vite-node scripts/build-pdfs.ts",
"build:sitemap": "vite-node scripts/build-sitemap.ts",
"prepare": "husky"
},
"publishConfig": {
Expand Down Expand Up @@ -61,6 +62,7 @@
"npm-run-all2": "^7.0.2",
"prettier": "^3.4.2",
"puppeteer": "^23.11.1",
"sitemap": "^8.0.0",
"typescript": "~5.7.2",
"vite": "^6.0.6",
"vite-node": "^2.1.8",
Expand Down
37 changes: 37 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
User-agent: *
Allow: /

Sitemap: https://how-to.lookscanned.io/sitemap.xml
48 changes: 48 additions & 0 deletions scripts/build-sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { SitemapStream, streamToPromise } from 'sitemap'
import { markdowns } from '../src/locales/how-to-use/markdowns'
import { writeFile } from 'fs/promises'
import { execSync } from 'child_process'

const langs = Object.keys(markdowns) as (keyof typeof markdowns)[]
const commitTimestamp = execSync('git log -1 --format=%at').toString().trim()
const commitDate = new Date(parseInt(commitTimestamp) * 1000)
const lastmod = commitDate.toISOString()

const smStream = new SitemapStream({
hostname: 'https://how-to.lookscanned.io',
lastmodDateOnly: false,
xmlns: {
// trim the xml namespace
news: false, // flip to false to omit the xml namespace for news
xhtml: true,
image: false,
video: false,
},
})

const xmlBuffer = new Promise<Buffer>((resolve, reject) => {
streamToPromise(smStream).then(resolve).catch(reject)
})

smStream.write({
url: 'https://how-to.lookscanned.io',
lastmod,
})

for (const lang of langs) {
smStream.write({
url: `https://how-to.lookscanned.io/how-to-use/${lang}`,
lastmod,
})
smStream.write({
url: `https://how-to.lookscanned.io/pdfs/how-to-use/${lang}.pdf`,
lastmod,
})
}

smStream.end()

const buffer = await xmlBuffer

// write to ./public/sitemap.xml
await writeFile('./public/sitemap.xml', buffer)

0 comments on commit be52597

Please sign in to comment.