@tenjuu99/blog 0.1.8 → 0.1.9
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/README.md +2 -0
- package/helper/index.js +63 -0
- package/lib/filter.js +6 -45
- package/lib/replaceVariablesFilter.js +54 -0
- package/lib/server.js +2 -1
- package/package.json +1 -1
- package/src-sample/pages/index.md +1 -28
- package/src-sample/template/default.html +2 -5
- package/src-sample/template/index.html +0 -3
package/README.md
CHANGED
package/helper/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { allData } from "../lib/indexer.js";
|
|
2
2
|
import { replaceVariablesFilter } from "../lib/filter.js";
|
|
3
|
+
import config from '../lib/config.js'
|
|
3
4
|
|
|
4
5
|
export function readIndex (filter = null) {
|
|
5
6
|
const data = Object.entries(allData)
|
|
@@ -51,3 +52,65 @@ export function indexedItems() {
|
|
|
51
52
|
indexedItemsSorted = sorted
|
|
52
53
|
return indexedItemsSorted
|
|
53
54
|
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 配列を再帰的に順不同リストに変換する
|
|
58
|
+
* @param {Array|string} arrayOrText
|
|
59
|
+
* @returns {mixed}
|
|
60
|
+
*/
|
|
61
|
+
export function arrayToList(arrayOrText) {
|
|
62
|
+
if (typeof arrayOrText === 'string') {
|
|
63
|
+
return `<li>${arrayOrText}</li>`
|
|
64
|
+
}
|
|
65
|
+
if (Array.isArray(arrayOrText)) {
|
|
66
|
+
let resultListText = '<ul>'
|
|
67
|
+
for (const item of arrayOrText) {
|
|
68
|
+
if (Array.isArray(item)) {
|
|
69
|
+
resultListText += `<li>${arrayToList(item)}</li>`
|
|
70
|
+
} else {
|
|
71
|
+
resultListText += `<li>${item}</li>`
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
resultListText += '</ul>'
|
|
75
|
+
arrayOrText = resultListText
|
|
76
|
+
}
|
|
77
|
+
return arrayOrText
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function renderIndex(pages, nodate = 'nodate', headingTag = 'h3') {
|
|
81
|
+
if (!pages) {
|
|
82
|
+
pages = readIndex()
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const renderList = {}
|
|
86
|
+
for (const page of pages) {
|
|
87
|
+
if (page.index) {
|
|
88
|
+
const url = config.relative_path ? config.relative_path + page.url : page.url
|
|
89
|
+
if (page.published === '1970-01-01') {
|
|
90
|
+
if (!renderList[nodate]) {
|
|
91
|
+
renderList[nodate] = []
|
|
92
|
+
}
|
|
93
|
+
renderList[nodate].push(`<a href="${url}">${page.title}</a>`)
|
|
94
|
+
continue
|
|
95
|
+
}
|
|
96
|
+
const published = new Date(page.published)
|
|
97
|
+
const year = `${published.getFullYear()}年`
|
|
98
|
+
const date = `${published.getMonth() +1}月${published.getDate()}日`
|
|
99
|
+
if (!renderList[year]) {
|
|
100
|
+
renderList[year] = []
|
|
101
|
+
}
|
|
102
|
+
renderList[year].push(`<a href="${url}">${page.title}</a> (${date})`)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const resultText = []
|
|
107
|
+
for (const key in renderList) {
|
|
108
|
+
resultText.push(`<${headingTag}>${key}</${headingTag}>`)
|
|
109
|
+
if (Array.isArray(renderList[key])) {
|
|
110
|
+
resultText.push(arrayToList(renderList[key]))
|
|
111
|
+
} else {
|
|
112
|
+
resultText.push(`<p>${renderList[key]}</p>`)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return resultText.join('\n')
|
|
116
|
+
}
|
package/lib/filter.js
CHANGED
|
@@ -2,37 +2,7 @@ import helper from '../lib/helper.js'
|
|
|
2
2
|
import includeFilter from './includeFilter.js'
|
|
3
3
|
import { srcDir } from './dir.js'
|
|
4
4
|
import config from './config.js'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @param {string} text
|
|
8
|
-
* @params {object} variables
|
|
9
|
-
* @return {text}
|
|
10
|
-
*/
|
|
11
|
-
const replaceVariablesFilter = (text, variables) => {
|
|
12
|
-
const matched = [...text.matchAll(/(\\)?{{\s?([\w\d\s,-_\(\)]+)\s?}}/g)]
|
|
13
|
-
const replace = Object.fromEntries(matched.map(match => [match[2].toLowerCase(), {replace: match[0], backslash: match[1]}]))
|
|
14
|
-
let replaced = text
|
|
15
|
-
for (const elm in replace) {
|
|
16
|
-
const toBeReplace = replace[elm].replace
|
|
17
|
-
const toBeReplaceScript = toBeReplace.match(/([\w\d_]+)\((.*)\)/)
|
|
18
|
-
|
|
19
|
-
if (replace[elm].backslash) { // escape variable syntax
|
|
20
|
-
const removeBackslash = replace[elm].replace.replace(/\\/, '')
|
|
21
|
-
replaced = replaced.replaceAll(toBeReplace, removeBackslash)
|
|
22
|
-
} else if (toBeReplaceScript) { // execute helper
|
|
23
|
-
const func = toBeReplaceScript[1]
|
|
24
|
-
const args = toBeReplaceScript[2].split(',').map(v => variables[v.trim()] ?? undefined)
|
|
25
|
-
if (!helper[func]) {
|
|
26
|
-
throw new Error('helper function is missing. function name: ' + func);
|
|
27
|
-
}
|
|
28
|
-
const replaceText = helper[func].call(null, ...args)
|
|
29
|
-
replaced = replaced.replaceAll(toBeReplace, replaceText)
|
|
30
|
-
} else {
|
|
31
|
-
replaced = replaced.replaceAll(toBeReplace, variables[elm])
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return replaced
|
|
35
|
-
}
|
|
5
|
+
import replaceVariablesFilter from './replaceVariablesFilter.js'
|
|
36
6
|
|
|
37
7
|
/**
|
|
38
8
|
* @param {string} condition
|
|
@@ -136,22 +106,13 @@ const replaceScriptFilter = async (text, variables) => {
|
|
|
136
106
|
if (result instanceof Promise) {
|
|
137
107
|
result = await result
|
|
138
108
|
}
|
|
139
|
-
|
|
140
|
-
|
|
109
|
+
switch (typeof result) {
|
|
110
|
+
case 'undefined':
|
|
111
|
+
case 'null':
|
|
112
|
+
result = ''
|
|
141
113
|
}
|
|
142
114
|
if (Array.isArray(result)) {
|
|
143
|
-
result = arrayToList(result)
|
|
144
|
-
} else if (typeof result === 'object') {
|
|
145
|
-
const resultText = []
|
|
146
|
-
for (const key in result) {
|
|
147
|
-
resultText.push(`<h3>${key}</h3>`)
|
|
148
|
-
if (Array.isArray(result[key])) {
|
|
149
|
-
resultText.push(arrayToList(result[key]))
|
|
150
|
-
} else {
|
|
151
|
-
resultText.push(`<p>${result[key]}</p>`)
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
result = resultText.join('\n')
|
|
115
|
+
result = helper.arrayToList(result)
|
|
155
116
|
}
|
|
156
117
|
replaced = replaced.replace(script.replace, result)
|
|
157
118
|
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import helper from './helper.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* テキストに含まれる変数を一括変換する
|
|
5
|
+
* ヘルパー処理もここで行われる
|
|
6
|
+
*
|
|
7
|
+
* @param {string} text
|
|
8
|
+
* @params {object} variables
|
|
9
|
+
* @return {text}
|
|
10
|
+
*/
|
|
11
|
+
const replaceVariablesFilter = (text, variables) => {
|
|
12
|
+
const matched = [...text.matchAll(/(\\)?{{[\s]*([^{}]+)[\s]*}}/g)]
|
|
13
|
+
const replace = Object.fromEntries(matched.map(match => [match[0], {variableName: match[2].trim().toLowerCase(), backslash: !!match[1]}]))
|
|
14
|
+
let replaced = text
|
|
15
|
+
for (const elm in replace) {
|
|
16
|
+
const [toBeReplace, replacedText] = replaceVariable(replace[elm].variableName, elm, replace[elm].backslash, variables)
|
|
17
|
+
replaced = replaced.replaceAll(toBeReplace, replacedText)
|
|
18
|
+
}
|
|
19
|
+
return replaced
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {string} target
|
|
24
|
+
* @param {string} replaceTargetRawText
|
|
25
|
+
* @param {boolean} backslash
|
|
26
|
+
* @param {object} variables
|
|
27
|
+
* @return {Array.<string>}
|
|
28
|
+
*/
|
|
29
|
+
const replaceVariable = (target, replaceTargetRawText, backslash, variables) => {
|
|
30
|
+
const toBeReplace = replaceTargetRawText
|
|
31
|
+
const toBeReplaceScript = toBeReplace.match(/([\w\d_]+)\((.*)\)/)
|
|
32
|
+
if (backslash) { // escape variable syntax
|
|
33
|
+
const removeBackslash = replaceTargetRawText.replace(/\\/, '')
|
|
34
|
+
return [replaceTargetRawText, removeBackslash]
|
|
35
|
+
} else if (toBeReplaceScript) { // execute helper
|
|
36
|
+
const func = toBeReplaceScript[1]
|
|
37
|
+
const args = toBeReplaceScript[2].split(',').map(v => {
|
|
38
|
+
const val = v.trim()
|
|
39
|
+
const argAsString = val.match(/^['"](.+)['"]$/)
|
|
40
|
+
if (argAsString) {
|
|
41
|
+
return argAsString[1]
|
|
42
|
+
}
|
|
43
|
+
return variables[val] ?? undefined
|
|
44
|
+
})
|
|
45
|
+
if (!helper[func]) {
|
|
46
|
+
throw new Error('helper function is missing. function name: ' + func);
|
|
47
|
+
}
|
|
48
|
+
const replaceText = helper[func].call(null, ...args)
|
|
49
|
+
return [replaceTargetRawText, replaceText]
|
|
50
|
+
}
|
|
51
|
+
return [replaceTargetRawText, variables[target]]
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export default replaceVariablesFilter
|
package/lib/server.js
CHANGED
|
@@ -33,7 +33,8 @@ const contentType = (ext) => {
|
|
|
33
33
|
const server = () => {
|
|
34
34
|
return http.createServer((request, response) => {
|
|
35
35
|
const url = new URL(`http://${request.headers.host}${request.url}`)
|
|
36
|
-
|
|
36
|
+
const isIndex = url.pathname.match(/(.+)?\/$/)
|
|
37
|
+
let path = isIndex ? `${url.pathname}index.html` : decodeURIComponent(url.pathname)
|
|
37
38
|
if (!path.includes('.')) {
|
|
38
39
|
path += '.html'
|
|
39
40
|
}
|
package/package.json
CHANGED
|
@@ -2,34 +2,7 @@
|
|
|
2
2
|
title: INDEX
|
|
3
3
|
template: index.html
|
|
4
4
|
index: false
|
|
5
|
-
url: /
|
|
6
|
-
published: 2023-03-03 20:21
|
|
7
|
-
modified: 2023-03-03 20:21
|
|
8
5
|
---
|
|
9
6
|
## INDEX
|
|
10
7
|
|
|
11
|
-
{
|
|
12
|
-
const data = helper.readIndex()
|
|
13
|
-
|
|
14
|
-
const pages = {}
|
|
15
|
-
for (const page of data) {
|
|
16
|
-
if (page.index) {
|
|
17
|
-
const url = variables.relative_path ? variables.relative_path + page.url : page.url
|
|
18
|
-
if (page.published === '1970-01-01') {
|
|
19
|
-
if (!pages['日付なし']) {
|
|
20
|
-
pages['日付なし'] = []
|
|
21
|
-
}
|
|
22
|
-
pages['日付なし'].push(`<a href="${url}">${page.title}</a>`)
|
|
23
|
-
continue
|
|
24
|
-
}
|
|
25
|
-
const published = new Date(page.published)
|
|
26
|
-
const year = `${published.getFullYear()}年`
|
|
27
|
-
const date = `${published.getMonth() +1}月${published.getDate()}日`
|
|
28
|
-
if (!pages[year]) {
|
|
29
|
-
pages[year] = []
|
|
30
|
-
}
|
|
31
|
-
pages[year].push(`<a href="${url}">${page.title}</a> (${date})`)
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return pages
|
|
35
|
-
{/script}
|
|
8
|
+
{{ renderIndex(null, "日付なし") }}
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
<!DOCTYPE html>
|
|
2
2
|
<html lang="{{LANG}}">
|
|
3
3
|
<head prefix="og: http://ogp.me/ns#">
|
|
4
|
-
{if gtag_id}
|
|
5
|
-
{include('template/gtag.html')}
|
|
6
|
-
{/if}
|
|
7
4
|
<meta charset="UTF-8">
|
|
8
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
9
6
|
<title>{{TITLE}} | {{SITE_NAME}}</title>
|
|
@@ -37,9 +34,9 @@
|
|
|
37
34
|
<h1>{{TITLE}}</h1>
|
|
38
35
|
{if published != '1970-01-01'}
|
|
39
36
|
<aside class="published">
|
|
40
|
-
<p>投稿: {
|
|
37
|
+
<p>投稿: {{ dateFormat(published) }}
|
|
41
38
|
{/if}
|
|
42
|
-
{if modified} / 更新: {
|
|
39
|
+
{if modified} / 更新: {{ dateFormat(modified) }}{/if}
|
|
43
40
|
{if published}
|
|
44
41
|
</p>
|
|
45
42
|
</aside>
|