@tenjuu99/blog 0.3.0 → 0.3.1

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.
@@ -11,6 +11,9 @@ import config from './config.js'
11
11
  let cacheBuster = {}
12
12
  const cacheBusterQuery = 't'
13
13
 
14
+ // 正規表現をモジュールレベルでキャッシュ
15
+ const CSS_LINK_REGEXP = /\${([\w-_/]+\.css)<<([\w-_/,.]+\.css)}/g
16
+
14
17
  /**
15
18
  * @param {string} src
16
19
  * @param {string} dist
@@ -51,7 +54,7 @@ const cssGenerator = async (src, dist) => {
51
54
  * @return {string}
52
55
  */
53
56
  const applyCss = async (text) => {
54
- const target = [...text.matchAll(/\${([\w-_/]+\.css)<<([\w-_/,.]+\.css)}/g)].map(val => {
57
+ const target = [...text.matchAll(CSS_LINK_REGEXP)].map(val => {
55
58
  return { matched: val[0], dist: val[1], src: val[2] }
56
59
  })
57
60
  for (const cssDist of target) {
package/lib/filter.js CHANGED
@@ -1,5 +1,12 @@
1
1
  import helper from '../lib/helper.js'
2
2
 
3
+ // 正規表現をモジュールレベルでキャッシュ
4
+ const IF_REGEXP = /(\{|\<)if\s(?<condition>[\s\S]+?)(}|>)(?<content>[\s\S]*?)((\{|\<)else(\}|\>)(?<elsecontent>[\s\S]*?))?(\{|\<)\/if(\>|})/g
5
+ const SCRIPT_REGEXP = /({script}|\<script\s.*type="ssg".*>)(?<script>[\s\S]*?)(\{\/script}|\<\/script>)/g
6
+ const CONDITION_SEGMENTS_REGEXP = /(?<left>[\S]+)\s(?<operator>!=|==)\s(?<right>[\S]+)/
7
+ const FUNCTION_CALL_REGEXP = /([\w]+)\((.*)\)/
8
+ const FUNCTION_ARG_REGEXP = /^(['"])?\s*([\w]+)(["'])?$/
9
+
3
10
  /**
4
11
  * @param {string} condition
5
12
  * @params {object} variables
@@ -7,7 +14,7 @@ import helper from '../lib/helper.js'
7
14
  */
8
15
  const ifConditionEvaluator = (condition, variables) => {
9
16
  if (condition.includes('=')) {
10
- const segmented = condition.match(/(?<left>[\S]+)\s(?<operator>!=|==)\s(?<right>[\S]+)/)
17
+ const segmented = condition.match(CONDITION_SEGMENTS_REGEXP)
11
18
  let {left, operator, right} = segmented.groups
12
19
  if (variables.hasOwnProperty(left)) {
13
20
  left = variables[left]
@@ -34,14 +41,14 @@ const ifConditionEvaluator = (condition, variables) => {
34
41
  return left != right
35
42
  }
36
43
  } else {
37
- const match = condition.match(/([\w]+)\((.*)\)/)
44
+ const match = condition.match(FUNCTION_CALL_REGEXP)
38
45
  if (match) {
39
46
  const func = match[1]
40
47
  if (helper[func] instanceof Function) {
41
48
  let args = match[2].trim()
42
49
  if (args) {
43
50
  args = args.split(',').map(arg => {
44
- const match = arg.match(/^(['"])?\s*([\w]+)(["'])?$/)
51
+ const match = arg.match(FUNCTION_ARG_REGEXP)
45
52
  if (match) {
46
53
  return match[1] ? `${match[2]}` : variables[match[2]]
47
54
  }
@@ -64,8 +71,7 @@ const ifConditionEvaluator = (condition, variables) => {
64
71
  * @returns {string}
65
72
  */
66
73
  const replaceIfFilter = (text, variables) => {
67
- const ifRegexp = new RegExp(/(\{|\<)if\s(?<condition>[\s\S]+?)(}|>)(?<content>[\s\S]*?)((\{|\<)else(\}|\>)(?<elsecontent>[\s\S]*?))?(\{|\<)\/if(\>|})/g)
68
- const matched = [...text.matchAll(ifRegexp)]
74
+ const matched = [...text.matchAll(IF_REGEXP)]
69
75
  for (const item of matched) {
70
76
  const target = item[0]
71
77
  const content = item.groups.content
@@ -83,8 +89,7 @@ const replaceIfFilter = (text, variables) => {
83
89
 
84
90
  const replaceScriptFilter = async (text, variables) => {
85
91
  let replaced = text
86
- const scriptRegexp = new RegExp(/({script}|\<script\s.*type="ssg".*>)(?<script>[\s\S]*?)(\{\/script}|\<\/script>)/g)
87
- const scripts = [...text.matchAll(scriptRegexp)].map((matched) => {
92
+ const scripts = [...text.matchAll(SCRIPT_REGEXP)].map((matched) => {
88
93
  return {
89
94
  replace: matched[0],
90
95
  script: matched.groups.script.trim("\n"),
@@ -4,11 +4,12 @@ import { staticFile } from './files.js'
4
4
 
5
5
  const alreadyLoaded = {}
6
6
 
7
- const includeRegexp = new RegExp(/\{\s*include\('(template|css)\/([\w\./-]+)'\)\s*\}/g)
7
+ // 正規表現をモジュールレベルでキャッシュ
8
+ const INCLUDE_REGEXP = /\{\s*include\('(template|css)\/([\w\./-]+)'\)\s*\}/g
8
9
 
9
10
  const includeFilter = (text) => {
10
11
  let replaced = text
11
- const include = [...text.matchAll(includeRegexp)].map(matched => {
12
+ const include = [...text.matchAll(INCLUDE_REGEXP)].map(matched => {
12
13
  return { toBeReplace: matched[0], type: matched[1], filename: matched[2] }
13
14
  })
14
15
  if (include.length === 0) {
@@ -27,7 +28,7 @@ const includeFilter = (text) => {
27
28
  throw new Error(cacheKey + ' is invalid')
28
29
  }
29
30
  // include を再帰的に解決する
30
- if (content.match(includeRegexp)) {
31
+ if (content.match(INCLUDE_REGEXP)) {
31
32
  content = includeFilter(content)
32
33
  }
33
34
  alreadyLoaded[cacheKey] = content
package/lib/pageData.js CHANGED
@@ -1,15 +1,18 @@
1
1
  "use strict"
2
2
  import config from './config.js'
3
3
 
4
+ // 正規表現をモジュールレベルでキャッシュ
5
+ const FRONTMATTER_REGEXP = /^(<!|-)--(?<variables>[\s\S]*?)--(-|>)/
6
+ const METADATA_KEY_REGEXP = /^([a-zA-Z\d_-]+):/
7
+
4
8
  const makePageData = (filename, content) => {
5
9
  const [name, ext] = filename.split('.')
6
10
  return parse(content, name, ext)
7
11
  }
8
12
 
9
13
  const parse = (content, name, ext) => {
10
- const regexp = new RegExp(/^(<!|-)--(?<variables>[\s\S]*?)--(-|>)/)
11
- const matched = content.match(regexp)
12
- const markdownReplaced = content.replace(regexp, '')
14
+ const matched = content.match(FRONTMATTER_REGEXP)
15
+ const markdownReplaced = content.replace(FRONTMATTER_REGEXP, '')
13
16
  const fullUrl = (data) => {
14
17
  const base = data.url_base + (data.relative_path ?? '')
15
18
  let url = data.url
@@ -64,7 +67,7 @@ const parseMetaData = (data) => {
64
67
  let key, value
65
68
  for (const line of data.split('\n')) {
66
69
  if (!isStringContinue) {
67
- const match = line.match(/^([a-zA-Z\d_-]+):/)
70
+ const match = line.match(METADATA_KEY_REGEXP)
68
71
  if (match) {
69
72
  key = match[1]
70
73
  value = line.slice(line.indexOf(':') + 1).trim()
@@ -1,5 +1,8 @@
1
1
  import helper from './helper.js'
2
2
 
3
+ // 正規表現をモジュールレベルでキャッシュ
4
+ const VARIABLE_REGEXP = /(\\)?{{[\s]*([^{}]+)[\s]*}}/g
5
+
3
6
  /**
4
7
  * テキストに含まれる変数を一括変換する
5
8
  * ヘルパー処理もここで行われる
@@ -9,7 +12,7 @@ import helper from './helper.js'
9
12
  * @return {text}
10
13
  */
11
14
  const replaceVariablesFilter = (text, variables) => {
12
- const matched = [...text.matchAll(/(\\)?{{[\s]*([^{}]+)[\s]*}}/g)]
15
+ const matched = [...text.matchAll(VARIABLE_REGEXP)]
13
16
  const replace = Object.fromEntries(matched.map(match => [match[0], {variableName: match[2].trim().toLowerCase(), backslash: !!match[1]}]))
14
17
  let replaced = text
15
18
  for (const elm in replace) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tenjuu99/blog",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "blog template",
5
5
  "main": "index.js",
6
6
  "scripts": {