@tenjuu99/blog 0.1.3 → 0.1.4

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.
@@ -38,7 +38,7 @@ jobs:
38
38
  run: npm install --omit=dev
39
39
 
40
40
  - name: Build
41
- run: cp .env.prod.sample .env && npm run generate
41
+ run: cp blog.json.prod blog.json && npm run generate
42
42
 
43
43
  - name: Setup Pages
44
44
  uses: actions/configure-pages@v4
package/bin/new ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env bash
2
+
3
+ mkdir -p "$(pwd)/src/pages"
4
+ echo "create src/pages"
5
+ mkdir -p "$(pwd)/src/template"
6
+ echo "create src/template"
7
+ mkdir -p "$(pwd)/src/css"
8
+ echo "create src/css"
9
+ mkdir -p "$(pwd)/src/image"
10
+ echo "create src/image"
11
+
12
+ mkdir "$(pwd)/.cache"
13
+ echo "{}" > "$(pwd)/.cache/index.json"
14
+
15
+ echo 'dist/*
16
+ node_modules/
17
+ .env
18
+ .cache
19
+ ' >> .gitignore
20
+
21
+ echo '{
22
+ "site_name": "rewrite here",
23
+ "url_base": "http://localhost:8000",
24
+ "src_dir": "src",
25
+ "dist_dir": "dist",
26
+ "distribute_raw": "image"
27
+ }' > "$(pwd)/blog.json"
28
+ echo "create blog.json"
package/blog.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "site_name": "test",
3
+ "url_base": "http://localhost:8000",
4
+ "src_dir": "src-sample",
5
+ "dist_dir": "dist",
6
+ "distribute_raw": "image",
7
+ "helper": "helper/index.js"
8
+ }
package/blog.json.prod ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "site_name": "blog/sample",
3
+ "url_base": "https://amashigeseiji.github.io/tenjuu99-blog",
4
+ "relative_path": "/tenjuu99-blog",
5
+ "src_dir": "src-sample",
6
+ "dist_dir": "dist",
7
+ "distribute_raw": "image",
8
+ "helper": "helper/index.js"
9
+ }
package/lib/config.js ADDED
@@ -0,0 +1,32 @@
1
+ import { readFileSync, existsSync } from 'node:fs'
2
+
3
+ const rootDir = process.cwd()
4
+ const config = {
5
+ "site_name": "default",
6
+ "url_base": "http://localhost:8000",
7
+ "src_dir": "src",
8
+ "dist_dir": "dist",
9
+ "distribute_raw": "image",
10
+ "relative_path": "",
11
+ "helper": ""
12
+ }
13
+ try {
14
+ const file = rootDir + '/blog.json'
15
+ if (existsSync(file)) {
16
+ const configOverride = JSON.parse(readFileSync(file, 'utf8'))
17
+ for (const item in configOverride) {
18
+ config[item] = configOverride[item]
19
+ }
20
+ }
21
+ const keys = Object.keys(process.env)
22
+ for (const item in config) {
23
+ const upper = item.toUpperCase()
24
+ if (keys.includes(upper)) {
25
+ config[item] = process.env[upper]
26
+ }
27
+ }
28
+ } catch (e) {
29
+ console.log(e)
30
+ }
31
+
32
+ export default config
@@ -6,6 +6,7 @@ import path from 'path'
6
6
  import { distDir as distRoot, cssDir } from './dir.js'
7
7
  import { watchers } from './watcher.js'
8
8
  import { styleText } from 'node:util'
9
+ import config from './config.js'
9
10
 
10
11
  let cacheBuster = {}
11
12
  const cacheBusterQuery = 't'
@@ -55,7 +56,7 @@ const applyCss = async (text) => {
55
56
  })
56
57
  for (const cssDist of target) {
57
58
  const cacheBuster = await cssGenerator(cssDist.src, cssDist.dist)
58
- text = text.replace(cssDist.matched, `${process.env.RELATIVE_PATH || ''}${cssDist.dist}?${cacheBusterQuery}=${cacheBuster}`)
59
+ text = text.replace(cssDist.matched, `${config.relative_path}${cssDist.dist}?${cacheBusterQuery}=${cacheBuster}`)
59
60
  }
60
61
  return text
61
62
  }
package/lib/dir.js CHANGED
@@ -1,6 +1,8 @@
1
+ import config from './config.js'
2
+
1
3
  const rootDir = process.cwd()
2
- const srcDir = `${rootDir}/${process.env.SRC_DIR}`
3
- const distDir = `${rootDir}/${process.env.DIST_DIR}`
4
+ const srcDir = `${rootDir}/${config.src_dir}`
5
+ const distDir = `${rootDir}/${config.dist_dir}`
4
6
  const pageDir = `${srcDir}/pages`
5
7
  const templateDir = `${srcDir}/template`
6
8
  const cssDir = `${srcDir}/css`
package/lib/distribute.js CHANGED
@@ -4,6 +4,7 @@ import path from 'path'
4
4
  import { minifyHtml } from './minify.js'
5
5
  import render from './render.js'
6
6
  import { styleText } from 'node:util'
7
+ import config from './config.js'
7
8
 
8
9
  const distribute = async (data, deleted, srcDir, distDir) => {
9
10
  if (deleted) {
@@ -22,7 +23,7 @@ const distribute = async (data, deleted, srcDir, distDir) => {
22
23
  console.log(styleText('green', '[generate]'), writeTo)
23
24
  })
24
25
  }
25
- const distributeRaw = process.env.DISTRIBUTE_RAW.split(',')
26
+ const distributeRaw = config.distribute_raw.split(',')
26
27
  distributeRaw.forEach((copyDir) => {
27
28
  fs.readdir(`${srcDir}/${copyDir}/`).then(async files => {
28
29
  await fs.stat(`${distDir}/${copyDir}/`).catch(async err => await fs.mkdir(`${distDir}/${copyDir}/`))
package/lib/filter.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import * as helper from '../helper/index.js'
2
2
  import includeFilter from './includeFilter.js'
3
3
  import { srcDir } from './dir.js'
4
+ import config from './config.js'
5
+ console.log(config)
4
6
 
5
7
  /**
6
8
  * @param {string} text
@@ -129,8 +131,8 @@ const replaceScriptFilter = async (text, variables) => {
129
131
  for (const script of scripts) {
130
132
  let helperMerged = {...helper}
131
133
  // env.HELPER が定義されていれば追加ヘルパーとして扱う
132
- if (process.env.HELPER) {
133
- const additional = await import(`${srcDir}/${process.env.HELPER}`)
134
+ if (config.helper) {
135
+ const additional = await import(`${srcDir}/${config.helper}`)
134
136
  helperMerged = Object.assign(helperMerged, additional)
135
137
  }
136
138
  let result = new Function('helper', 'variables', script.script)(helperMerged, variables)
package/lib/indexer.js CHANGED
@@ -1,30 +1,42 @@
1
1
  "use strict"
2
- import fs from "node:fs/promises";
2
+ import { writeFile, readFile } from "node:fs/promises";
3
+ import { readdirSync, existsSync, mkdirSync } from "node:fs";
3
4
  import { pageDir, cacheDir } from './dir.js'
4
5
  import makePageData from './pageData.js'
5
6
 
6
7
  const indexFile = `${cacheDir}/index.json`
7
8
 
9
+ let newIndex = []
8
10
  let allData = {}
9
11
  let deleted = []
10
12
 
11
- const indexing = async () => {
12
- const oldIndex = await fs.readFile(indexFile, 'utf8').then(text => JSON.parse(text)).catch(error => [])
13
+ const collect = (dir, files = {}, namePrefix = '') => {
14
+ const dirents = readdirSync(dir, { withFileTypes: true })
15
+ dirents.forEach((dirent) => {
16
+ if (dirent.isDirectory()) {
17
+ collect(`${dirent.path}/${dirent.name}`, files, namePrefix + dirent.name + '/')
18
+ } else {
19
+ if (dirent.name.match(/\.(md|html)$/)) {
20
+ const pageData = makePageData(`${namePrefix}${dirent.name}`)
21
+ allData[pageData.name] = pageData
22
+ const { name, url, __output } = pageData
23
+ newIndex.push({ name, url, __output })
24
+ }
25
+ }
26
+ })
27
+ }
13
28
 
14
- const newIndex = []
29
+ const indexing = async () => {
30
+ newIndex = []
15
31
  allData = {}
16
32
  deleted = []
17
- await fs.readdir(pageDir).then(files => {
18
- files
19
- .filter(fileName => fileName.match('\.(md|html)$'))
20
- .forEach(file => {
21
- const metaData = makePageData(file)
22
- allData[metaData.name] = metaData
23
- const { name, url, __output } = metaData
24
- newIndex.push({ name, url, __output })
25
- })
26
- })
27
- fs.writeFile(indexFile, JSON.stringify(newIndex))
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))
28
40
 
29
41
  // 旧インデックスから差分を計算して削除対象をピックアップする
30
42
  deleted = oldIndex.filter(oi => !newIndex.map(ni => ni.__output).includes(oi.__output))
package/lib/pageData.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict"
2
2
  import fs from "node:fs";
3
3
  import { pageDir } from './dir.js'
4
+ import config from './config.js'
4
5
 
5
6
  const load = (path) => {
6
7
  return fs.readFileSync(path, 'utf8')
@@ -26,11 +27,11 @@ const parse = (content, name, ext) => {
26
27
  index: true,
27
28
  noindex: false,
28
29
  lang: 'ja',
29
- site_name: process.env.SITE_NAME,
30
- url_base: process.env.URL_BASE,
31
- gtag_id: process.env.GTAG_ID,
30
+ site_name: config.site_name,
31
+ url_base: config.url_base,
32
+ gtag_id: config.gtag_id,
32
33
  markdown: markdownReplaced,
33
- relative_path: process.env.RELATIVE_PATH || '',
34
+ relative_path: config.relative_path || '',
34
35
  template: 'default.html',
35
36
  ext: 'html',
36
37
  __output: `/${name}.html`,
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "@tenjuu99/blog",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "blog template",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "dev": "eval $(cat .env | tr \"\n\" \" \") node bin/server",
8
- "generate": "eval $(cat .env | tr \"\n\" \" \") node bin/generate",
7
+ "dev": "node bin/server",
8
+ "generate": "node bin/generate",
9
9
  "test": "echo \"Error: no test specified\" && exit 1"
10
10
  },
11
11
  "bin": {
12
12
  "generate": "bin/generate",
13
- "server": "bin/server"
13
+ "server": "bin/server",
14
+ "new": "bin/new"
14
15
  },
15
16
  "author": "AmashigeSeiji",
16
17
  "repository": {
@@ -4,3 +4,8 @@ url: /abc
4
4
  published: 2024-08-31
5
5
  ---
6
6
  hoge
7
+ hoge
8
+
9
+
10
+ abc
11
+ dc
File without changes
@@ -0,0 +1 @@
1
+ これは [post](/post) 配下にあります
File without changes