Skip to content

Commit 3755639

Browse files
committed
fix: use frontmatter format for metadata in storage
1 parent 0eac789 commit 3755639

File tree

4 files changed

+26
-51
lines changed

4 files changed

+26
-51
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test: ## Run code linting tests
2424
pug-lint server/views && jest
2525

2626
docker-dev-up: ## Run dockerized dev environment
27-
docker-compose -f ./dev/docker-${DEVDB}/docker-compose.yml -p wiki --project-directory . up -d
27+
docker-compose -f ./dev/docker-${DEVDB}/docker-compose.yml -p wiki --project-directory . up -d --remove-orphans
2828
docker-compose -f ./dev/docker-${DEVDB}/docker-compose.yml -p wiki --project-directory . exec wiki yarn dev
2929

3030
docker-dev-down: ## Shutdown dockerized dev environment

server/models/pages.js

+21
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,27 @@ module.exports = class Page extends Model {
114114
})
115115
}
116116

117+
/**
118+
* Inject page metadata into contents
119+
*/
120+
injectMetadata () {
121+
let meta = [
122+
['title', this.title],
123+
['description', this.description],
124+
['published', this.isPublished.toString()],
125+
['date', this.updatedAt],
126+
['tags', '']
127+
]
128+
switch (this.contentType) {
129+
case 'markdown':
130+
return '---\n' + meta.map(mt => `${mt[0]}: ${mt[1]}`).join('\n') + '\n---\n\n' + this.content
131+
case 'html':
132+
return '<!--\n' + meta.map(mt => `${mt[0]}: ${mt[1]}`).join('\n') + '\n-->\n\n' + this.content
133+
default:
134+
return this.content
135+
}
136+
}
137+
117138
static async createPage(opts) {
118139
await WIKI.models.pages.query().insert({
119140
authorId: opts.authorId,

server/modules/storage/disk/storage.js

+2-25
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,6 @@ const getFileExtension = (contentType) => {
1515
}
1616
}
1717

18-
/**
19-
* Inject page metadata into contents
20-
*/
21-
const injectMetadata = (page) => {
22-
let meta = [
23-
['title', page.title],
24-
['description', page.description]
25-
]
26-
let metaFormatted = ''
27-
switch (page.contentType) {
28-
case 'markdown':
29-
metaFormatted = meta.map(mt => `[//]: # ${mt[0]}: ${mt[1]}`).join('\n')
30-
break
31-
case 'html':
32-
metaFormatted = meta.map(mt => `<!-- ${mt[0]}: ${mt[1]} -->`).join('\n')
33-
break
34-
default:
35-
metaFormatted = meta.map(mt => `#WIKI ${mt[0]}: ${mt[1]}`).join('\n')
36-
break
37-
}
38-
return `${metaFormatted}\n\n${page.content}`
39-
}
40-
4118
module.exports = {
4219
async activated() {
4320
// not used
@@ -56,12 +33,12 @@ module.exports = {
5633
async created(page) {
5734
WIKI.logger.info(`(STORAGE/DISK) Creating file ${page.path}...`)
5835
const filePath = path.join(this.config.path, `${page.path}.${getFileExtension(page.contentType)}`)
59-
await fs.outputFile(filePath, injectMetadata(page), 'utf8')
36+
await fs.outputFile(filePath, page.injectMetadata(), 'utf8')
6037
},
6138
async updated(page) {
6239
WIKI.logger.info(`(STORAGE/DISK) Updating file ${page.path}...`)
6340
const filePath = path.join(this.config.path, `${page.path}.${getFileExtension(page.contentType)}`)
64-
await fs.outputFile(filePath, injectMetadata(page), 'utf8')
41+
await fs.outputFile(filePath, page.injectMetadata(), 'utf8')
6542
},
6643
async deleted(page) {
6744
WIKI.logger.info(`(STORAGE/DISK) Deleting file ${page.path}...`)

server/modules/storage/git/storage.js

+2-25
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,6 @@ const getFileExtension = (contentType) => {
1717
}
1818
}
1919

20-
/**
21-
* Inject page metadata into contents
22-
*/
23-
const injectMetadata = (page) => {
24-
let meta = [
25-
['title', page.title],
26-
['description', page.description]
27-
]
28-
let metaFormatted = ''
29-
switch (page.contentType) {
30-
case 'markdown':
31-
metaFormatted = meta.map(mt => `[//]: # ${mt[0]}: ${mt[1]}`).join('\n')
32-
break
33-
case 'html':
34-
metaFormatted = meta.map(mt => `<!-- ${mt[0]}: ${mt[1]} -->`).join('\n')
35-
break
36-
default:
37-
metaFormatted = meta.map(mt => `#WIKI ${mt[0]}: ${mt[1]}`).join('\n')
38-
break
39-
}
40-
return `${metaFormatted}\n\n${page.content}`
41-
}
42-
4320
module.exports = {
4421
git: null,
4522
repoPath: path.join(process.cwd(), 'data/repo'),
@@ -131,7 +108,7 @@ module.exports = {
131108
WIKI.logger.info(`(STORAGE/GIT) Committing new file ${page.path}...`)
132109
const fileName = `${page.path}.${getFileExtension(page.contentType)}`
133110
const filePath = path.join(this.repoPath, fileName)
134-
await fs.outputFile(filePath, injectMetadata(page), 'utf8')
111+
await fs.outputFile(filePath, page.injectMetadata(), 'utf8')
135112

136113
await this.git.add(`./${fileName}`)
137114
await this.git.commit(`docs: create ${page.path}`, fileName, {
@@ -142,7 +119,7 @@ module.exports = {
142119
WIKI.logger.info(`(STORAGE/GIT) Committing updated file ${page.path}...`)
143120
const fileName = `${page.path}.${getFileExtension(page.contentType)}`
144121
const filePath = path.join(this.repoPath, fileName)
145-
await fs.outputFile(filePath, injectMetadata(page), 'utf8')
122+
await fs.outputFile(filePath, page.injectMetadata(), 'utf8')
146123

147124
await this.git.add(`./${fileName}`)
148125
await this.git.commit(`docs: update ${page.path}`, fileName, {

0 commit comments

Comments
 (0)