Skip to content

Commit 81fe2db

Browse files
committed
Basic blog support
1 parent 62af972 commit 81fe2db

9 files changed

Lines changed: 86 additions & 33 deletions

File tree

lib/app/app.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,6 @@ export function createApp () {
6969
}
7070
})
7171

72-
// redirect /foo to /foo/
73-
router.beforeEach((to, from, next) => {
74-
if (!/(\/|\.html)$/.test(to.path)) {
75-
next(Object.assign({}, to, {
76-
path: to.path + '/'
77-
}))
78-
} else {
79-
next()
80-
}
81-
})
82-
8372
const options = {}
8473

8574
themeEnhanceApp({ Vue, options, router, siteData })

lib/build.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
5353

5454
// create server renderer using built manifests
5555
const renderer = createBundleRenderer(serverBundle, {
56-
clientManifest,
56+
// clientManifest,
5757
runInNewContext: false,
5858
inject: false,
5959
shouldPrefetch: options.siteConfig.shouldPrefetch || (() => true),
@@ -89,6 +89,12 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
8989
})
9090
}
9191

92+
// write rss
93+
const rssPath = path.resolve(outDir, 'feed.xml')
94+
const rssContent = renderRss(options)
95+
96+
await fs.writeFile(rssPath, rssContent)
97+
9298
// DONE.
9399
const relativeDir = path.relative(process.cwd(), outDir)
94100
logger.success(`\n${chalk.green('Success!')} Generated static files in ${chalk.cyan(relativeDir)}.\n`)
@@ -158,8 +164,13 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
158164
console.error(logger.error(chalk.red(`Error rendering ${pagePath}:`), false))
159165
throw e
160166
}
161-
const filename = decodeURIComponent(pagePath.replace(/\/$/, '/index.html').replace(/^\//, ''))
162-
const filePath = path.resolve(outDir, filename)
167+
let outPath = pagePath
168+
if (!pagePath.endsWith('/') && !pagePath.endsWith('.html')) {
169+
outPath += '.html'
170+
}
171+
const filename = decodeURIComponent(outPath.replace(/\/$/, '/index.html').replace(/^\//, ''))
172+
filePath = path.resolve(outDir, filename)
173+
163174
await fs.ensureDir(path.dirname(filePath))
164175
await fs.writeFile(filePath, html)
165176
}
@@ -193,3 +204,25 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
193204
await fs.writeFile(appChunkPath, styleChunkContent + appChunkContent)
194205
}
195206
}
207+
208+
function renderRss (options) {
209+
const sortedRssItems = options.rssItems.sort((a, b) => a > b).reverse()
210+
return `<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
211+
<channel>
212+
<title>${options.siteConfig.title}</title>
213+
<link>http://blog.matsu.io</link>
214+
${sortedRssItems.map(renderRssItem).join('')}
215+
</channel>
216+
</rss>
217+
`
218+
}
219+
220+
function renderRssItem (rssItem) {
221+
return `
222+
<item>
223+
<title>${rssItem.title}</title>
224+
<link>http://blog.matsu.io${rssItem.path}</link>
225+
<pubDate>${rssItem.date}</pubDate>
226+
</item>
227+
`
228+
}

lib/markdown/imgWrapper.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = md => {
2+
const defaultRenderer = md.renderer.rules.image
3+
4+
md.renderer.rules.image = (tokens, idx, options, env, self) => {
5+
return `<figure>${defaultRenderer(tokens, idx, options, env, self)}</figure>`
6+
}
7+
}

lib/markdown/index.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
const highlight = require('./highlight')
2-
const highlightLines = require('./highlightLines')
3-
const preWrapper = require('./preWrapper')
2+
const preWrapper = require('./preWrapper.js')
3+
const imgWrapper = require('./imgWrapper.js')
44
const lineNumbers = require('./lineNumbers')
55
const component = require('./component')
66
const hoistScriptStyle = require('./hoist')
7-
const convertRouterLink = require('./link')
87
const containers = require('./containers')
98
const snippet = require('./snippet')
109
const emoji = require('markdown-it-emoji')
@@ -24,13 +23,9 @@ module.exports = ({ markdown = {}} = {}) => {
2423
})
2524
// custom plugins
2625
.use(component)
27-
.use(highlightLines)
2826
.use(preWrapper)
27+
.use(imgWrapper)
2928
.use(snippet)
30-
.use(convertRouterLink, Object.assign({
31-
target: '_blank',
32-
rel: 'noopener noreferrer'
33-
}, markdown.externalLinks))
3429
.use(hoistScriptStyle)
3530
.use(containers)
3631

lib/markdown/lineNumbers.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ module.exports = md => {
1919

2020
const finalCode = rawCode
2121
.replace('<!--beforeend-->', `${lineNumbersWrapperCode}<!--beforeend-->`)
22-
.replace('extra-class', 'line-numbers-mode')
2322

2423
return finalCode
2524
}

lib/markdown/link.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module.exports = (md, externalAttrs) => {
7575
if (hasOpenExternalLink) {
7676
hasOpenExternalLink = false
7777
// add OutBoundLink to the beforeend of this link if it opens in _blank.
78-
return '<OutboundLink/>' + self.renderToken(tokens, idx, options)
78+
return self.renderToken(tokens, idx, options)
7979
}
8080
return self.renderToken(tokens, idx, options)
8181
}

lib/markdown/preWrapper.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ module.exports = md => {
1313
const [tokens, idx] = args
1414
const token = tokens[idx]
1515
const rawCode = fence(...args)
16-
return `<!--beforebegin--><div class="language-${token.info.trim()} extra-class">` +
17-
`<!--afterbegin-->${rawCode}<!--beforeend--></div><!--afterend-->`
16+
return `<!--beforebegin--><!--afterbegin-->${rawCode}<!--beforeend--><!--afterend-->`
1817
}
1918
}

lib/prepare/codegen.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@ exports.genRoutesFile = async function ({
66
sourceDir,
77
pageFiles
88
}) {
9-
function genRoute ({ path: pagePath, key: componentName }, index) {
9+
const rssItems = []
10+
11+
function genRoute ({ path: pagePath, key: componentName, frontmatter }, index) {
1012
const file = pageFiles[index]
1113
const filePath = path.resolve(sourceDir, file)
14+
15+
let cleanPagePath = pagePath.replace(/.html/g, '')
16+
if (cleanPagePath !== '/' && cleanPagePath.endsWith('/')) {
17+
cleanPagePath = cleanPagePath.slice(0, -1)
18+
}
1219
let code = `
1320
{
1421
name: ${JSON.stringify(componentName)},
15-
path: ${JSON.stringify(pagePath)},
22+
path: ${JSON.stringify(cleanPagePath)},
1623
component: ThemeLayout,
1724
beforeEnter: (to, from, next) => {
1825
import(${JSON.stringify(filePath)}).then(comp => {
@@ -22,20 +29,28 @@ exports.genRoutesFile = async function ({
2229
}
2330
}`
2431

32+
if (!frontmatter.top_page) {
33+
rssItems.push({
34+
title: frontmatter.title,
35+
path: cleanPagePath,
36+
date: frontmatter.date
37+
})
38+
}
39+
2540
const dncodedPath = decodeURIComponent(pagePath)
2641
if (dncodedPath !== pagePath) {
2742
code += `,
2843
{
2944
path: ${JSON.stringify(dncodedPath)},
30-
redirect: ${JSON.stringify(pagePath)}
45+
redirect: ${JSON.stringify(cleanPagePath)}
3146
}`
3247
}
3348

3449
if (/\/$/.test(pagePath)) {
3550
code += `,
3651
{
3752
path: ${JSON.stringify(pagePath + 'index.html')},
38-
redirect: ${JSON.stringify(pagePath)}
53+
redirect: ${JSON.stringify(cleanPagePath)}
3954
}`
4055
}
4156

@@ -48,15 +63,22 @@ exports.genRoutesFile = async function ({
4863
component: ThemeNotFound
4964
}`
5065

51-
return (
66+
const generatedRoutes = pages.map(genRoute)
67+
68+
const routesCode = (
5269
`import ThemeLayout from '@themeLayout'\n` +
5370
`import ThemeNotFound from '@themeNotFound'\n` +
5471
`import { injectMixins } from '@app/util'\n` +
5572
`import rootMixins from '@app/root-mixins'\n\n` +
5673
`injectMixins(ThemeLayout, rootMixins)\n` +
5774
`injectMixins(ThemeNotFound, rootMixins)\n\n` +
58-
`export const routes = [${pages.map(genRoute).join(',')}${notFoundRoute}\n]`
75+
`export const routes = [${generatedRoutes.join(',')}${notFoundRoute}\n]`
5976
)
77+
78+
return {
79+
routesCode,
80+
rssItems
81+
}
6082
}
6183

6284
exports.genComponentRegistrationFile = async function ({ sourceDir }) {

lib/prepare/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,24 @@ module.exports = async function prepare (sourceDir) {
99
const options = await resolveOptions(sourceDir)
1010

1111
// 2. generate routes & user components registration code
12-
const routesCode = await genRoutesFile(options)
12+
const { routesCode, rssItems } = await genRoutesFile(options)
1313
const componentCode = await genComponentRegistrationFile(options)
14+
options.rssItems = rssItems
1415

1516
await writeTemp('routes.js', [
1617
componentCode,
1718
routesCode
1819
].join('\n'))
1920

2021
// 3. generate siteData
22+
options.siteData.pages.forEach(p => {
23+
if (p.path) {
24+
p.path = p.path.replace(/.html$/g, '')
25+
}
26+
if (p.path !== '/' && p.path.endsWith('/')) {
27+
p.path = p.path.slice(0, -1)
28+
}
29+
})
2130
const dataCode = `export const siteData = ${JSON.stringify(options.siteData, null, 2)}`
2231
await writeTemp('siteData.js', dataCode)
2332

0 commit comments

Comments
 (0)