@tenjuu99/blog 0.1.9 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/applyTemplate.js +13 -5
- package/lib/cssGenerator.js +6 -5
- package/lib/distribute.js +40 -11
- package/lib/files.js +43 -0
- package/lib/filter.js +1 -3
- package/lib/generate.js +8 -5
- package/lib/includeFilter.js +15 -15
- package/lib/indexer.js +19 -24
- package/lib/pageData.js +1 -14
- package/lib/render.js +8 -4
- package/package.json +1 -1
- package/src-sample/pages/post/1.md +1 -0
- package/src-sample/pages/post/2.md +1 -0
- package/src-sample/pages/post/3.md +1 -0
- package/src-sample/pages/post/index.md +7 -0
package/lib/applyTemplate.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict"
|
|
2
|
-
import fs from "node:fs/promises";
|
|
3
2
|
import applyCss from './cssGenerator.js'
|
|
4
|
-
import
|
|
3
|
+
import includeFilter from './includeFilter.js'
|
|
5
4
|
import { templateDir, cssDir } from './dir.js'
|
|
5
|
+
import { staticFile, staticFiles, warmUp } from './files.js'
|
|
6
6
|
import { watchers } from './watcher.js'
|
|
7
7
|
|
|
8
8
|
let templates = {}
|
|
@@ -11,16 +11,24 @@ const applyTemplate = async (name = 'default.html') => {
|
|
|
11
11
|
if (templates[name]) {
|
|
12
12
|
return templates[name]
|
|
13
13
|
}
|
|
14
|
-
let templateContent = await
|
|
15
|
-
templateContent =
|
|
14
|
+
let templateContent = await staticFile(`template/${name}`)
|
|
15
|
+
templateContent = includeFilter(templateContent)
|
|
16
16
|
templateContent = await applyCss(templateContent)
|
|
17
17
|
templates[name] = templateContent
|
|
18
18
|
return templateContent
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
const warmUpTemplate = async () => {
|
|
22
|
+
await warmUp()
|
|
23
|
+
const templates = staticFiles()
|
|
24
|
+
.filter(file => file[0].indexOf('template/') === 0)
|
|
25
|
+
.map(f => applyTemplate(f[0].split('/')[1]))
|
|
26
|
+
await Promise.all(templates)
|
|
27
|
+
}
|
|
28
|
+
|
|
21
29
|
watchers.push({
|
|
22
30
|
paths: [cssDir, templateDir],
|
|
23
31
|
callback: () => { templates = {} }
|
|
24
32
|
})
|
|
25
33
|
|
|
26
|
-
export
|
|
34
|
+
export { applyTemplate, warmUpTemplate }
|
package/lib/cssGenerator.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict"
|
|
2
2
|
import fs from "node:fs/promises";
|
|
3
|
+
import { staticFile } from './files.js'
|
|
3
4
|
import { minifyCss } from './minify.js'
|
|
4
5
|
import { createHash } from 'crypto'
|
|
5
6
|
import path from 'path'
|
|
6
|
-
import { distDir
|
|
7
|
+
import { distDir, cssDir } from './dir.js'
|
|
7
8
|
import { watchers } from './watcher.js'
|
|
8
9
|
import { styleText } from 'node:util'
|
|
9
10
|
import config from './config.js'
|
|
@@ -26,14 +27,14 @@ const cssGenerator = async (src, dist) => {
|
|
|
26
27
|
}
|
|
27
28
|
let css = ''
|
|
28
29
|
for (const cssFile of src.split(',')) {
|
|
29
|
-
css +=
|
|
30
|
+
css += staticFile(`css/${cssFile}`)
|
|
30
31
|
}
|
|
31
32
|
css = minifyCss(css)
|
|
32
33
|
cacheBuster[key] = createHash('md5').update(css).digest('hex')
|
|
33
34
|
|
|
34
|
-
return await fs.mkdir(`${
|
|
35
|
-
fs.writeFile(`${
|
|
36
|
-
console.log(styleText('green', '[generate]'), `${src} => ${
|
|
35
|
+
return await fs.mkdir(`${distDir}${path.dirname(dist)}`, { recursive: true }).then(() => {
|
|
36
|
+
fs.writeFile(`${distDir}${dist}`, css)
|
|
37
|
+
console.log(styleText('green', '[generate]'), `${src} => ${distDir}${dist}`)
|
|
37
38
|
return cacheBuster[key]
|
|
38
39
|
})
|
|
39
40
|
}
|
package/lib/distribute.js
CHANGED
|
@@ -1,23 +1,33 @@
|
|
|
1
1
|
"use strict"
|
|
2
2
|
import fs from "node:fs/promises";
|
|
3
|
+
import { existsSync, mkdirSync } from "node:fs";
|
|
3
4
|
import path from 'path'
|
|
4
5
|
import { minifyHtml } from './minify.js'
|
|
5
6
|
import render from './render.js'
|
|
6
7
|
import { styleText } from 'node:util'
|
|
7
8
|
import config from './config.js'
|
|
9
|
+
import { cacheDir } from './dir.js'
|
|
10
|
+
import { applyTemplate, warmUpTemplate } from './applyTemplate.js'
|
|
8
11
|
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
const indexFile = `${cacheDir}/index.json`
|
|
13
|
+
|
|
14
|
+
const renderPage = async (page) => {
|
|
15
|
+
const template = page.template
|
|
16
|
+
return [page.name, await render(template, page)]
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const distribute = async (data, srcDir, distDir) => {
|
|
20
|
+
await warmUpTemplate()
|
|
21
|
+
const promises = []
|
|
22
|
+
const newIndex = []
|
|
17
23
|
for (const name in data) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
24
|
+
promises.push(renderPage(data[name]))
|
|
25
|
+
newIndex.push({ name: data.name, url: data.url, __output: data.__output })
|
|
26
|
+
}
|
|
27
|
+
const renderedString = await Promise.all(promises)
|
|
28
|
+
for (const page of renderedString) {
|
|
29
|
+
const [ pageName, rendered ] = page
|
|
30
|
+
let writeTo = `${distDir}${data[pageName].__output}`
|
|
21
31
|
fs.mkdir(path.dirname(writeTo), { recursive: true}).then(() => {
|
|
22
32
|
fs.writeFile(writeTo, minifyHtml(rendered))
|
|
23
33
|
console.log(styleText('green', '[generate]'), writeTo)
|
|
@@ -33,6 +43,25 @@ const distribute = async (data, deleted, srcDir, distDir) => {
|
|
|
33
43
|
})
|
|
34
44
|
})
|
|
35
45
|
})
|
|
46
|
+
|
|
47
|
+
if (!existsSync(cacheDir)) {
|
|
48
|
+
mkdirSync(cacheDir)
|
|
49
|
+
}
|
|
50
|
+
fs.readFile(indexFile, 'utf8')
|
|
51
|
+
.then(text => {
|
|
52
|
+
const oldIndex = JSON.parse(text)
|
|
53
|
+
deleted = oldIndex.filter(oi => !newIndex.map(ni => ni.__output).includes(oi.__output))
|
|
54
|
+
fs.writeFile(indexFile, JSON.stringify(newIndex))
|
|
55
|
+
if (deleted) {
|
|
56
|
+
for (const obj of deleted) {
|
|
57
|
+
console.log(styleText('red', '[unlink]'), `${distDir}${obj.__output}`)
|
|
58
|
+
fs.unlink(`${distDir}${obj.__output}`)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
.catch(error => {
|
|
63
|
+
fs.writeFile(indexFile, JSON.stringify(newIndex))
|
|
64
|
+
})
|
|
36
65
|
}
|
|
37
66
|
|
|
38
67
|
export default distribute
|
package/lib/files.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import { templateDir, cssDir } from './dir.js'
|
|
3
|
+
import { watchers } from './watcher.js'
|
|
4
|
+
|
|
5
|
+
let staticFilesContainer = {}
|
|
6
|
+
let loaded = false
|
|
7
|
+
|
|
8
|
+
const warmUp = async () => {
|
|
9
|
+
if (loaded) {
|
|
10
|
+
return
|
|
11
|
+
}
|
|
12
|
+
const templateFiles = await fs.readdir(templateDir).then(files => files.map(f => [`template/${f}`, `${templateDir}/${f}`]))
|
|
13
|
+
const cssFiles = await fs.readdir(cssDir).then(files => files.map(f => [`css/${f}`, `${cssDir}/${f}`]))
|
|
14
|
+
const files = [...templateFiles, ...cssFiles]
|
|
15
|
+
const loadFiles = files.map(file => fs.readFile(file[1], 'utf8').then(content => [file[0], content]))
|
|
16
|
+
staticFilesContainer = Object.fromEntries(await Promise.all(loadFiles))
|
|
17
|
+
loaded = true
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const staticFile = (name) => {
|
|
21
|
+
if (!loaded) {
|
|
22
|
+
throw new Error('not initialized')
|
|
23
|
+
}
|
|
24
|
+
if (staticFilesContainer[name]) {
|
|
25
|
+
return staticFilesContainer[name]
|
|
26
|
+
}
|
|
27
|
+
if (name.indexOf('template/') === 0) {
|
|
28
|
+
return fs.readFile(templateDir + '/' + name.replace('template/', ''))
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const staticFiles = () => {
|
|
33
|
+
return Object.entries(staticFilesContainer)
|
|
34
|
+
}
|
|
35
|
+
watchers.push({
|
|
36
|
+
paths: [cssDir, templateDir],
|
|
37
|
+
callback: async () => {
|
|
38
|
+
loaded = false
|
|
39
|
+
await warmUp()
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
export { staticFile, staticFiles, warmUp }
|
package/lib/filter.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import helper from '../lib/helper.js'
|
|
2
|
-
import includeFilter from './includeFilter.js'
|
|
3
2
|
import { srcDir } from './dir.js'
|
|
4
3
|
import config from './config.js'
|
|
5
4
|
import replaceVariablesFilter from './replaceVariablesFilter.js'
|
|
@@ -122,6 +121,5 @@ const replaceScriptFilter = async (text, variables) => {
|
|
|
122
121
|
export {
|
|
123
122
|
replaceIfFilter,
|
|
124
123
|
replaceScriptFilter,
|
|
125
|
-
replaceVariablesFilter
|
|
126
|
-
includeFilter
|
|
124
|
+
replaceVariablesFilter
|
|
127
125
|
}
|
package/lib/generate.js
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
"use strict"
|
|
2
2
|
import distribute from './distribute.js'
|
|
3
|
-
import { indexing, allData
|
|
3
|
+
import { indexing, allData } from './indexer.js'
|
|
4
4
|
import { srcDir, distDir } from './dir.js'
|
|
5
5
|
import { styleText } from 'node:util'
|
|
6
6
|
|
|
7
7
|
const generate = async () => {
|
|
8
|
-
|
|
8
|
+
let start = performance.now()
|
|
9
9
|
await indexing()
|
|
10
|
+
let end = performance.now()
|
|
11
|
+
console.log(styleText('blue', '[indexing: ' + (end - start) + "ms]"))
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
start = performance.now()
|
|
14
|
+
await distribute(allData, srcDir, distDir)
|
|
15
|
+
end = performance.now()
|
|
16
|
+
console.log(styleText('blue', '[distribute: ' + (end - start) + "ms]"))
|
|
14
17
|
}
|
|
15
18
|
|
|
16
19
|
export default generate
|
package/lib/includeFilter.js
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import fs from "node:fs/promises";
|
|
2
1
|
import { minifyCss } from './minify.js'
|
|
3
2
|
import { templateDir, cssDir } from './dir.js'
|
|
3
|
+
import { staticFile } from './files.js'
|
|
4
4
|
|
|
5
5
|
const alreadyLoaded = {}
|
|
6
6
|
|
|
7
|
-
const
|
|
7
|
+
const includeRegexp = new RegExp(/\{\s*include\('(template|css)\/([\w\./]+)'\)\s*\}/g)
|
|
8
|
+
|
|
9
|
+
const includeFilter = (text) => {
|
|
8
10
|
let replaced = text
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
+
const include = [...text.matchAll(includeRegexp)].map(matched => {
|
|
12
|
+
return { toBeReplace: matched[0], type: matched[1], filename: matched[2] }
|
|
13
|
+
})
|
|
14
|
+
if (include.length === 0) {
|
|
15
|
+
return replaced
|
|
16
|
+
}
|
|
11
17
|
for (const index in include) {
|
|
12
|
-
const
|
|
18
|
+
const {toBeReplace, type, filename} = include[index]
|
|
13
19
|
let content
|
|
14
20
|
const cacheKey = `${type}/${filename}`
|
|
15
21
|
if (!alreadyLoaded[cacheKey]) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
break
|
|
20
|
-
case 'css':
|
|
21
|
-
content = await fs.readFile(`${cssDir}/${filename}`, 'utf8')
|
|
22
|
-
break
|
|
23
|
-
default:
|
|
24
|
-
throw new Error('type does not match neither `template` nor `css`.');
|
|
22
|
+
content = staticFile(cacheKey)
|
|
23
|
+
if (typeof content === 'undefined') {
|
|
24
|
+
throw new Error(cacheKey + ' is not found')
|
|
25
25
|
}
|
|
26
26
|
// include を再帰的に解決する
|
|
27
27
|
if (content.match(includeRegexp)) {
|
|
28
|
-
content =
|
|
28
|
+
content = includeFilter(content)
|
|
29
29
|
}
|
|
30
30
|
alreadyLoaded[cacheKey] = content
|
|
31
31
|
} else {
|
package/lib/indexer.js
CHANGED
|
@@ -1,45 +1,40 @@
|
|
|
1
1
|
"use strict"
|
|
2
|
-
import {
|
|
3
|
-
import { readdirSync
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { readdirSync } from "node:fs";
|
|
4
4
|
import { pageDir, cacheDir } from './dir.js'
|
|
5
5
|
import makePageData from './pageData.js'
|
|
6
6
|
|
|
7
|
-
const indexFile = `${cacheDir}/index.json`
|
|
8
|
-
|
|
9
|
-
let newIndex = []
|
|
10
7
|
let allData = {}
|
|
11
|
-
let deleted = []
|
|
12
8
|
|
|
13
|
-
|
|
9
|
+
/**
|
|
10
|
+
* @param {string} dir
|
|
11
|
+
* @param {string} namePrefix
|
|
12
|
+
* @param {Array<Promise>} promises
|
|
13
|
+
* @return {Promise<string[]>[]}
|
|
14
|
+
*/
|
|
15
|
+
const collect = (dir, namePrefix = '', promises = []) => {
|
|
14
16
|
const dirents = readdirSync(dir, { withFileTypes: true })
|
|
15
17
|
dirents.forEach((dirent) => {
|
|
16
18
|
if (dirent.isDirectory()) {
|
|
17
|
-
collect(`${dirent.path}/${dirent.name}`,
|
|
19
|
+
collect(`${dirent.path}/${dirent.name}`, namePrefix + dirent.name + '/', promises)
|
|
18
20
|
} else {
|
|
19
21
|
if (dirent.name.match(/\.(md|html)$/)) {
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
const { name, url, __output } = pageData
|
|
23
|
-
newIndex.push({ name, url, __output })
|
|
22
|
+
const name = `${namePrefix}${dirent.name}`
|
|
23
|
+
promises.push(readFile(`${dir}/${dirent.name}`, 'utf8').then(f => [name, f]))
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
})
|
|
27
|
+
return promises
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
const indexing = async () => {
|
|
30
|
-
newIndex = []
|
|
31
31
|
allData = {}
|
|
32
|
-
deleted = []
|
|
33
|
-
if (!existsSync(cacheDir)) {
|
|
34
|
-
mkdirSync(cacheDir)
|
|
35
|
-
}
|
|
36
|
-
const oldIndex = await readFile(indexFile, 'utf8').then(text => JSON.parse(text)).catch(error => [])
|
|
37
|
-
|
|
38
|
-
collect(pageDir)
|
|
39
|
-
writeFile(indexFile, JSON.stringify(newIndex))
|
|
40
32
|
|
|
41
|
-
|
|
42
|
-
|
|
33
|
+
const files = await Promise.all(collect(pageDir))
|
|
34
|
+
files.forEach((file) => {
|
|
35
|
+
const pageData = makePageData(file[0], file[1])
|
|
36
|
+
allData[pageData.name] = pageData
|
|
37
|
+
})
|
|
43
38
|
}
|
|
44
39
|
|
|
45
|
-
export { indexing, allData
|
|
40
|
+
export { indexing, allData }
|
package/lib/pageData.js
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
1
|
"use strict"
|
|
2
|
-
import fs from "node:fs";
|
|
3
|
-
import { pageDir } from './dir.js'
|
|
4
2
|
import config from './config.js'
|
|
5
3
|
|
|
6
|
-
const
|
|
7
|
-
return fs.readFileSync(path, 'utf8')
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const makePageData = (filename) => {
|
|
11
|
-
const content = load(`${pageDir}/${filename}`)
|
|
4
|
+
const makePageData = (filename, content) => {
|
|
12
5
|
const [name, ext] = filename.split('.')
|
|
13
6
|
return parse(content, name, ext)
|
|
14
7
|
}
|
|
@@ -53,12 +46,6 @@ const parse = (content, name, ext) => {
|
|
|
53
46
|
})
|
|
54
47
|
)
|
|
55
48
|
const metaDataMerged = Object.assign(metaDataDefault, metaData)
|
|
56
|
-
if (!metaDataMerged.description) {
|
|
57
|
-
metaDataMerged.description = markdownReplaced.replace(/(<([^>]+)>)/gi, '').slice(0, 200).replaceAll("\n", '') + '...'
|
|
58
|
-
}
|
|
59
|
-
if (!metaDataMerged.og_description) {
|
|
60
|
-
metaDataMerged.og_description = metaDataMerged.og_description
|
|
61
|
-
}
|
|
62
49
|
metaDataMerged['__output'] = name === 'index' ? '/index.html' : `${metaDataMerged.url}.${metaDataMerged.ext}`
|
|
63
50
|
|
|
64
51
|
return metaDataMerged
|
package/lib/render.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
replaceIfFilter,
|
|
3
3
|
replaceScriptFilter,
|
|
4
|
-
replaceVariablesFilter
|
|
5
|
-
includeFilter
|
|
4
|
+
replaceVariablesFilter
|
|
6
5
|
} from './filter.js'
|
|
6
|
+
import includeFilter from './includeFilter.js'
|
|
7
7
|
import { marked } from "marked";
|
|
8
|
-
import applyTemplate from './applyTemplate.js'
|
|
8
|
+
import { applyTemplate } from './applyTemplate.js'
|
|
9
9
|
|
|
10
10
|
const render = async (templateName, data) => {
|
|
11
11
|
let template = await applyTemplate(templateName)
|
|
@@ -13,11 +13,15 @@ const render = async (templateName, data) => {
|
|
|
13
13
|
template = await replaceScriptFilter(template, data)
|
|
14
14
|
|
|
15
15
|
let markdown = data.markdown
|
|
16
|
-
markdown =
|
|
16
|
+
markdown = includeFilter(markdown)
|
|
17
17
|
markdown = await replaceIfFilter(markdown, data)
|
|
18
18
|
markdown = await replaceScriptFilter(markdown, data)
|
|
19
19
|
markdown = replaceVariablesFilter(markdown, data)
|
|
20
20
|
data.markdown = data.__filetype === 'md' ? marked.parse(markdown) : markdown
|
|
21
|
+
if (!data.description) {
|
|
22
|
+
data.description = data.markdown.replaceAll("\n", '').replace(/(<([^>]+)>)/gi, '').slice(0, 150) + '...'
|
|
23
|
+
data.og_description = data.description
|
|
24
|
+
}
|
|
21
25
|
|
|
22
26
|
return replaceVariablesFilter(template, data)
|
|
23
27
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
top > [post](/post/) > /post/1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
top > [post](/post/) > /post/2
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
top > [post](/post/) > /post/3
|