From 3c0c816e8e1363abe91786b16216548838d563b0 Mon Sep 17 00:00:00 2001 From: wherelse Date: Sat, 21 May 2022 16:05:21 +0800 Subject: [PATCH] add sitemap support for gridea --- package.json | 1 + src/server/renderer.ts | 53 ++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 27 +++++++++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/package.json b/package.json index f36fbc8b..6a6198bb 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "prismjs": "^1.16.0", "shortid": "^2.2.14", "simple-get": "^4.0.1", + "sitemap": "^7.1.1", "slug": "^0.9.3", "ssh2-sftp-client": "^4.2.4", "striptags": "^3.1.1", diff --git a/src/server/renderer.ts b/src/server/renderer.ts index 0ca927bf..85b2af74 100644 --- a/src/server/renderer.ts +++ b/src/server/renderer.ts @@ -7,6 +7,7 @@ import moment from 'moment' import less from 'less' import { Feed } from 'feed' import junk from 'junk' +import { SitemapStream, streamToPromise } from 'sitemap' import { wordCount, timeCalc } from '../helpers/words-count' import Model from './model' import ContentHelper from '../helpers/content-helper' @@ -77,6 +78,8 @@ export default class Renderer extends Model { await this.buildCname() await this.buildFeed() + + await this.buildSitemap() } /** @@ -590,6 +593,56 @@ export default class Renderer extends Model { fs.writeFileSync(urlJoin(this.outputDir, feedFilename), feed.atom1()) } + async buildSitemap() { + const sitemapFilename = 'sitemap.xml' + const { themeConfig } = this.db + + const sitemapStream = new SitemapStream({ + hostname: themeConfig.domain, + lastmodDateOnly: false, // print date not time + xmlns: { // trim the xml namespace + news: true, // flip to false to omit the xml namespace for news + xhtml: false, + image: true, + video: true, + custom: [ + 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"', + 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"', + ], + }, + }) + + const menusData = this.menuData + menusData.forEach((menu: IMenu) => { + sitemapStream.write({ + url: menu.link, + priority: 0.5, + }) + }) + + const postsData = this.postsData + .filter((item: IPostRenderData) => !item.hideInList) + postsData.forEach((post: IPostRenderData) => { + const prioritys = post.isTop ? 1 : 0.5 + sitemapStream.write({ + url: post.link, + priority: prioritys, + }) + }) + + const { tagsData } = this + tagsData.forEach((tag: ITagRenderData) => { + sitemapStream.write({ + url: tag.link, + priority: 0.5, + }) + }) + + sitemapStream.end() + const sitedata = await streamToPromise(sitemapStream) + fs.writeFileSync(urlJoin(this.outputDir, sitemapFilename), sitedata.toString()) + } + /** * Copy file to output folder */ diff --git a/yarn.lock b/yarn.lock index 1bff748e..4949c545 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1020,6 +1020,11 @@ resolved "https://registry.npmmirror.com/@types/node/-/node-12.20.50.tgz" integrity sha512-+9axpWx2b2JCVovr7Ilgt96uc6C1zBKOQMpGtRbWT9IoR/8ue32GGMfGA4woP8QyP2gBs6GQWEVM3tCybGCxDA== +"@types/node@^17.0.5": + version "17.0.35" + resolved "https://registry.npmjs.org/@types/node/-/node-17.0.35.tgz#635b7586086d51fb40de0a2ec9d1014a5283ba4a" + integrity sha512-vu1SrqBjbbZ3J6vwY17jBs8Sr/BKA+/a/WtjRG+whKg1iuLFOosq872EXS0eXWILdO36DHQQeku/ZcL6hz2fpg== + "@types/normalize-package-data@^2.4.0": version "2.4.0" resolved "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz" @@ -1044,6 +1049,13 @@ resolved "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.3.tgz" integrity sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA== +"@types/sax@^1.2.1": + version "1.2.4" + resolved "https://registry.npmjs.org/@types/sax/-/sax-1.2.4.tgz#8221affa7f4f3cb21abd22f244cfabfa63e6a69e" + integrity sha512-pSAff4IAxJjfAXUG6tFkO7dsSbTmf8CtUpfhhZ5VhkRpC4628tJhh3+V6H1E+/Gs9piSzYKT5yzHO5M4GG9jkw== + dependencies: + "@types/node" "*" + "@types/semver@^6.0.2": version "6.2.0" resolved "https://registry.npmjs.org/@types/semver/-/semver-6.2.0.tgz" @@ -2038,6 +2050,11 @@ arch@^2.1.1: resolved "http://registry.npm.taobao.org/arch/download/arch-2.1.1.tgz" integrity sha1-j1wnMao1owkpIhuwZA7tZRdeyE4= +arg@^5.0.0: + version "5.0.1" + resolved "https://registry.npmjs.org/arg/-/arg-5.0.1.tgz#eb0c9a8f77786cad2af8ff2b862899842d7b6adb" + integrity sha512-e0hDa9H2Z9AwFkk2qDlwhoMYE4eToKarchkQHovNdLTCYMHZHeRjI71crOh+dio4K6u1IcwubQqo79Ga4CyAQA== + argparse@^1.0.7: version "1.0.10" resolved "http://registry.npm.taobao.org/argparse/download/argparse-1.0.10.tgz" @@ -10130,6 +10147,16 @@ simple-swizzle@^0.2.2: dependencies: is-arrayish "^0.3.1" +sitemap@^7.1.1: + version "7.1.1" + resolved "https://registry.npmjs.org/sitemap/-/sitemap-7.1.1.tgz#eeed9ad6d95499161a3eadc60f8c6dce4bea2bef" + integrity sha512-mK3aFtjz4VdJN0igpIJrinf3EO8U8mxOPsTBzSsy06UtjZQJ3YY3o3Xa7zSc5nMqcMrRwlChHZ18Kxg0caiPBg== + dependencies: + "@types/node" "^17.0.5" + "@types/sax" "^1.2.1" + arg "^5.0.0" + sax "^1.2.4" + slash@^1.0.0: version "1.0.0" resolved "http://registry.npm.taobao.org/slash/download/slash-1.0.0.tgz"