mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
fix/file-write
This commit is contained in:
@@ -3,17 +3,22 @@ import fs from 'fs'
|
||||
import BLOG from '@/blog.config'
|
||||
|
||||
export async function generateRobotsTxt() {
|
||||
fs.mkdirSync('./public', { recursive: true })
|
||||
fs.writeFileSync('./public/robots.txt', `
|
||||
# *
|
||||
User-agent: *
|
||||
Allow: /
|
||||
|
||||
# Host
|
||||
Host: ${BLOG.LINK}
|
||||
|
||||
# Sitemaps
|
||||
Sitemap: ${BLOG.LINK}/sitemap.xml
|
||||
|
||||
`)
|
||||
const content = `
|
||||
# *
|
||||
User-agent: *
|
||||
Allow: /
|
||||
|
||||
# Host
|
||||
Host: ${BLOG.LINK}
|
||||
|
||||
# Sitemaps
|
||||
Sitemap: ${BLOG.LINK}/sitemap.xml
|
||||
|
||||
`
|
||||
try {
|
||||
fs.writeFileSync('robots.txt', content)
|
||||
fs.writeFileSync('./public/robots.txt', content)
|
||||
} catch (error) {
|
||||
console.warn('无法写入文件', error)
|
||||
}
|
||||
}
|
||||
|
||||
54
lib/rss.js
54
lib/rss.js
@@ -5,6 +5,26 @@ import ReactDOMServer from 'react-dom/server'
|
||||
import { getPostBlocks } from './notion'
|
||||
import NotionPage from '@/components/NotionPage'
|
||||
|
||||
/**
|
||||
* 生成RSS内容
|
||||
* @param {*} post
|
||||
* @returns
|
||||
*/
|
||||
const createFeedContent = async post => {
|
||||
// 加密的文章内容只返回摘要
|
||||
if (post.password && post.password !== '') {
|
||||
return post.summary
|
||||
}
|
||||
const blockMap = await getPostBlocks(post.id, 'rss-content')
|
||||
if (blockMap) {
|
||||
post.blockMap = blockMap
|
||||
const content = ReactDOMServer.renderToString(<NotionPage post={post} />)
|
||||
const regexExp =
|
||||
/<div class="notion-collection-row"><div class="notion-collection-row-body"><div class="notion-collection-row-property"><div class="notion-collection-column-title"><svg.*?class="notion-collection-column-title-icon">.*?<\/svg><div class="notion-collection-column-title-body">.*?<\/div><\/div><div class="notion-collection-row-value">.*?<\/div><\/div><\/div><\/div>/g
|
||||
return content.replace(regexExp, '')
|
||||
}
|
||||
}
|
||||
|
||||
export async function generateRss(posts) {
|
||||
const year = new Date().getFullYear()
|
||||
const feed = new Feed({
|
||||
@@ -30,28 +50,16 @@ export async function generateRss(posts) {
|
||||
})
|
||||
}
|
||||
|
||||
fs.mkdirSync('./public/rss', { recursive: true })
|
||||
fs.writeFileSync('./public/rss/feed.xml', feed.rss2())
|
||||
fs.writeFileSync('./public/rss/atom.xml', feed.atom1())
|
||||
fs.writeFileSync('./public/rss/feed.json', feed.json1())
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成RSS内容
|
||||
* @param {*} post
|
||||
* @returns
|
||||
*/
|
||||
const createFeedContent = async post => {
|
||||
// 加密的文章内容只返回摘要
|
||||
if (post.password && post.password !== '') {
|
||||
return post.summary
|
||||
}
|
||||
const blockMap = await getPostBlocks(post.id, 'rss-content')
|
||||
if (blockMap) {
|
||||
post.blockMap = blockMap
|
||||
const content = ReactDOMServer.renderToString(<NotionPage post={post} />)
|
||||
const regexExp =
|
||||
/<div class="notion-collection-row"><div class="notion-collection-row-body"><div class="notion-collection-row-property"><div class="notion-collection-column-title"><svg.*?class="notion-collection-column-title-icon">.*?<\/svg><div class="notion-collection-column-title-body">.*?<\/div><\/div><div class="notion-collection-row-value">.*?<\/div><\/div><\/div><\/div>/g
|
||||
return content.replace(regexExp, '')
|
||||
try {
|
||||
fs.mkdirSync('./public/rss', { recursive: true })
|
||||
fs.writeFileSync('./public/rss/feed.xml', feed.rss2())
|
||||
fs.writeFileSync('./public/rss/atom.xml', feed.atom1())
|
||||
fs.writeFileSync('./public/rss/feed.json', feed.json1())
|
||||
fs.mkdirSync('./rss', { recursive: true })
|
||||
fs.writeFileSync('./rss/feed.xml', feed.rss2())
|
||||
fs.writeFileSync('./rss/atom.xml', feed.atom1())
|
||||
fs.writeFileSync('./rss/feed.json', feed.json1())
|
||||
} catch (error) {
|
||||
console.warn('无法写入文件', error)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,49 +3,52 @@ import fs from 'fs'
|
||||
import BLOG from '@/blog.config'
|
||||
|
||||
export async function generateSitemapXml({ allPages }) {
|
||||
fs.mkdirSync('./public', { recursive: true })
|
||||
const urls = [{
|
||||
loc: `${BLOG.LINK}`,
|
||||
lastmod: new Date().toISOString().split('T')[0],
|
||||
changefreq: 'daily'
|
||||
}, {
|
||||
loc: `${BLOG.LINK}/archive`,
|
||||
lastmod: new Date().toISOString().split('T')[0],
|
||||
changefreq: 'daily'
|
||||
}, {
|
||||
loc: `${BLOG.LINK}/category`,
|
||||
lastmod: new Date().toISOString().split('T')[0],
|
||||
changefreq: 'daily'
|
||||
}, {
|
||||
loc: `${BLOG.LINK}/tag`,
|
||||
lastmod: new Date().toISOString().split('T')[0],
|
||||
changefreq: 'daily'
|
||||
}]
|
||||
|
||||
const urls = [{
|
||||
loc: `${BLOG.LINK}`,
|
||||
lastmod: new Date().toISOString().split('T')[0],
|
||||
changefreq: 'daily'
|
||||
}, {
|
||||
loc: `${BLOG.LINK}/archive`,
|
||||
lastmod: new Date().toISOString().split('T')[0],
|
||||
changefreq: 'daily'
|
||||
}, {
|
||||
loc: `${BLOG.LINK}/category`,
|
||||
lastmod: new Date().toISOString().split('T')[0],
|
||||
changefreq: 'daily'
|
||||
}, {
|
||||
loc: `${BLOG.LINK}/tag`,
|
||||
lastmod: new Date().toISOString().split('T')[0],
|
||||
changefreq: 'daily'
|
||||
}]
|
||||
|
||||
allPages?.forEach(post => {
|
||||
urls.push({
|
||||
loc: `${BLOG.LINK}/${post.slug}`,
|
||||
lastmod: new Date(post?.date?.start_date || post?.createdTime).toISOString().split('T')[0],
|
||||
changefreq: 'daily'
|
||||
allPages?.forEach(post => {
|
||||
urls.push({
|
||||
loc: `${BLOG.LINK}/${post.slug}`,
|
||||
lastmod: new Date(post?.date?.start_date || post?.createdTime).toISOString().split('T')[0],
|
||||
changefreq: 'daily'
|
||||
})
|
||||
})
|
||||
})
|
||||
const xml = createSitemapXml(urls)
|
||||
fs.writeFileSync('./public/sitemap.xml', xml)
|
||||
}
|
||||
const xml = createSitemapXml(urls)
|
||||
try {
|
||||
fs.writeFileSync('sitemap.xml', xml)
|
||||
fs.writeFileSync('./public/sitemap.xml', xml)
|
||||
} catch (error) {
|
||||
console.warn('无法写入文件', error)
|
||||
}
|
||||
|
||||
function createSitemapXml(urls) {
|
||||
let urlsXml = ''
|
||||
urls.forEach(u => {
|
||||
urlsXml += `<url>
|
||||
|
||||
function createSitemapXml(urls) {
|
||||
let urlsXml = ''
|
||||
urls.forEach(u => {
|
||||
urlsXml += `<url>
|
||||
<loc>${u.loc}</loc>
|
||||
<lastmod>${u.lastmod}</lastmod>
|
||||
<changefreq>${u.changefreq}</changefreq>
|
||||
</url>
|
||||
`
|
||||
})
|
||||
})
|
||||
|
||||
return `
|
||||
return `
|
||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
|
||||
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
|
||||
xmlns:xhtml="http://www.w3.org/1999/xhtml"
|
||||
@@ -54,4 +57,4 @@ function createSitemapXml(urls) {
|
||||
${urlsXml}
|
||||
</urlset>
|
||||
`
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user