Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add sitemap support for gridea #991

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
53 changes: 53 additions & 0 deletions src/server/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -77,6 +78,8 @@ export default class Renderer extends Model {
await this.buildCname()

await this.buildFeed()

await this.buildSitemap()
}

/**
Expand Down Expand Up @@ -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
*/
Expand Down
27 changes: 27 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down