@tenjuu99/blog 0.1.6 → 0.1.7
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/filter.js +14 -17
- package/lib/helper.js +12 -0
- package/lib/render.js +1 -0
- package/package.json +1 -1
- package/src-sample/pages/sample.md +3 -7
package/lib/filter.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
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
|
-
console.log(config)
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
7
|
* @param {string} text
|
|
@@ -10,19 +9,23 @@ console.log(config)
|
|
|
10
9
|
* @return {text}
|
|
11
10
|
*/
|
|
12
11
|
const replaceVariablesFilter = (text, variables) => {
|
|
13
|
-
const matched = [...text.matchAll(/{{\s?([\w\d
|
|
14
|
-
const replace = Object.fromEntries(matched.map(match => [match[
|
|
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]}]))
|
|
15
14
|
let replaced = text
|
|
16
15
|
for (const elm in replace) {
|
|
17
|
-
const toBeReplace = replace[elm]
|
|
18
|
-
const toBeReplaceScript = toBeReplace.match(/([\w\d_
|
|
19
|
-
|
|
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
|
|
20
23
|
const func = toBeReplaceScript[1]
|
|
21
|
-
const
|
|
24
|
+
const args = toBeReplaceScript[2].split(',').map(v => variables[v.trim()] ?? undefined)
|
|
22
25
|
if (!helper[func]) {
|
|
23
|
-
throw new
|
|
26
|
+
throw new Error('helper function is missing. function name: ' + func);
|
|
24
27
|
}
|
|
25
|
-
const replaceText = helper[func]
|
|
28
|
+
const replaceText = helper[func].call(null, ...args)
|
|
26
29
|
replaced = replaced.replaceAll(toBeReplace, replaceText)
|
|
27
30
|
} else {
|
|
28
31
|
replaced = replaced.replaceAll(toBeReplace, variables[elm])
|
|
@@ -129,13 +132,7 @@ const replaceScriptFilter = async (text, variables) => {
|
|
|
129
132
|
}
|
|
130
133
|
})
|
|
131
134
|
for (const script of scripts) {
|
|
132
|
-
let
|
|
133
|
-
// env.HELPER が定義されていれば追加ヘルパーとして扱う
|
|
134
|
-
if (config.helper) {
|
|
135
|
-
const additional = await import(`${srcDir}/${config.helper}`)
|
|
136
|
-
helperMerged = Object.assign(helperMerged, additional)
|
|
137
|
-
}
|
|
138
|
-
let result = new Function('helper', 'variables', script.script)(helperMerged, variables)
|
|
135
|
+
let result = new Function('helper', 'variables', script.script)(helper, variables)
|
|
139
136
|
if (result instanceof Promise) {
|
|
140
137
|
result = await result
|
|
141
138
|
}
|
package/lib/helper.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as helperDefault from '../helper/index.js'
|
|
2
|
+
import config from './config.js'
|
|
3
|
+
import { srcDir } from './dir.js'
|
|
4
|
+
|
|
5
|
+
let helper = {...helperDefault}
|
|
6
|
+
|
|
7
|
+
if (config['helper']) {
|
|
8
|
+
const helperAdditional = await import(`${srcDir}/${config.helper}`)
|
|
9
|
+
helper = Object.assign(helper, helperAdditional)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default helper
|
package/lib/render.js
CHANGED
|
@@ -16,6 +16,7 @@ const render = async (templateName, data) => {
|
|
|
16
16
|
markdown = await includeFilter(markdown)
|
|
17
17
|
markdown = await replaceIfFilter(markdown, data)
|
|
18
18
|
markdown = await replaceScriptFilter(markdown, data)
|
|
19
|
+
markdown = replaceVariablesFilter(markdown, data)
|
|
19
20
|
data.markdown = data.__filetype === 'md' ? marked.parse(markdown) : markdown
|
|
20
21
|
|
|
21
22
|
return replaceVariablesFilter(template, data)
|
package/package.json
CHANGED
|
@@ -113,7 +113,7 @@ return variables.someVariable
|
|
|
113
113
|
{/script}
|
|
114
114
|
```
|
|
115
115
|
|
|
116
|
-
##
|
|
116
|
+
## ヘルパー
|
|
117
117
|
|
|
118
118
|
ヘルパー関数を作成します。
|
|
119
119
|
```
|
|
@@ -132,14 +132,10 @@ HELPER=helper/index.js
|
|
|
132
132
|
追加したヘルパーを利用できます。
|
|
133
133
|
<pre>
|
|
134
134
|
// src-sample/pages/sample.md
|
|
135
|
-
|
|
136
|
-
return helper.additionalHelper()
|
|
137
|
-
</script>
|
|
135
|
+
\{{ additionalHelper() }}
|
|
138
136
|
</pre>
|
|
139
137
|
|
|
140
138
|
実際に出力させると次の行のとおりです。
|
|
141
139
|
```
|
|
142
|
-
|
|
143
|
-
return helper.additionalHelper()
|
|
144
|
-
</script>
|
|
140
|
+
{{additionalHelper()}}
|
|
145
141
|
```
|