This repository has been archived by the owner on Oct 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
nuxt.config.js
156 lines (145 loc) · 4.05 KB
/
nuxt.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const webpack = require('webpack')
const fs = require('fs-extra')
const path = require('path')
const lodash = require('lodash')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const domain = 'hire.jonasgalvez.com.br'
const isDirectory = (source) => {
return fs.lstatSync(source).isDirectory()
}
const parseEntry = (entryFile) => {
const raw = fs.readFileSync(entryFile, 'utf8')
let [
published,
summary
] = raw
.substr(0, raw.indexOf('#'))
.trim()
.split(/\n\n/)
const publishedText = published
published = new Date(Date.parse(published))
const md = raw.substr(raw.indexOf('#'))
const title = md
.substr(raw.indexOf('\n'))
.match(/^#\s+(.*)/)[1]
return {
title,
summary,
published,
publishedText,
id: `tag:${domain},${published.getFullYear()}:${published.getTime()}`,
markdown: entryFile.replace(/^.*\/entries\//, './entries/')
}
}
const loadEntry = (fullpath) => {
if (isDirectory(fullpath)) {
const entryFiles = fs.readdirSync(fullpath)
const text = entryFiles.find((f) => f.endsWith('.entry'))
if (!text) {
return false
}
const assetsRoot = path.join('static', 'entries')
fs.ensureDirSync(assetsRoot)
const others = entryFiles
.filter((f) => !f.endsWith('.entry'))
.forEach((f) => {
fs.copySync(
path.join(fullpath, f),
path.join(assetsRoot, f)
)
})
return parseEntry(path.join(fullpath, text))
} else if (fullpath.endsWith('.entry')) {
return parseEntry(fullpath)
} else {
return false
}
}
const generateEntryPermalink = (title, published) => {
const slug = title.replace(/\s+/g, '-')
const date = published.toString().split(/\s+/).slice(1, 4).reverse()
return `${date[0]}/${date[2]}/${date[1]}/${slug}`
}
const entries = (() => {
const entriesRoot = path.resolve(__dirname, 'entries')
const entries = []
const dirEntries = fs.readdirSync(entriesRoot)
for (let i = 0; i < dirEntries.length; i++) {
const validEntry = loadEntry(path.join(entriesRoot, dirEntries[i]))
if (validEntry) {
entries.push(
Object.assign({}, {
permalink: generateEntryPermalink(validEntry.title, validEntry.published),
published: validEntry.published.toISOString()
}, validEntry)
)
}
}
entries.sort((a, b) => {
return b.published - a.published
})
return entries
})()
const generateIndex = () => {
fs.writeFileSync(
path.resolve(__dirname, 'entries.json'),
JSON.stringify(entries, null, 2)
)
}
const generateFeeds = () => {
const rssFeedTemplate = lodash.template(
fs.readFileSync('./feeds/rss.xml.template', 'utf8')
)
const atomFeedTemplate = lodash.template(
fs.readFileSync('./feeds/atom.xml.template', 'utf8')
)
const data = {
entries: entries.slice(0, 10),
domain
}
fs.writeFileSync('./static/rss.xml', rssFeedTemplate(data))
fs.writeFileSync('./static/atom.xml', atomFeedTemplate(data))
}
const routes = require('./pages/index')
export default {
plugins: ['~/plugins/nuxpress.js'],
srcDir: './',
router: {
extendRoutes: (nuxtRoutes, resolve) => {
nuxtRoutes.splice(0, nuxtRoutes.length, ...routes.map((route) => {
return Object.assign({}, route, {
component: resolve(__dirname, route.component)
})
}))
}
},
build: {
babel: {
plugins: ['transform-do-expressions']
},
plugins: [
new webpack.IgnorePlugin(/^entries/),
new CopyWebpackPlugin([
{ from: 'entries/*', to: 'entries/' },
{ from: 'pages/*.md', to: 'pages/' }
])
],
extend (config, { isDev }) {
// Generate entries.json file with all entry metadata
generateIndex()
// Generate /static/atom.xml and /static/rss.xml
generateFeeds()
// Tweak for GitHub pages
if (!isDev) {
config.output.publicPath = './_nuxt/'
} else {
// Ensure linting on dev mode
config.module.rules.push({
enforce: 'pre',
test: /\.(js|vue)$/,
exclude: /(node_modules)/
})
}
}
}
}