@tenjuu99/blog 0.2.15 → 0.2.17

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/bin/dev-server CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import { spawn } from 'child_process'
4
- import { srcDir, watch as watchDir } from '../lib/dir.js'
4
+ import { srcDir, watch as watchDir, packageDir, packageDirCore } from '../lib/dir.js'
5
5
  import { watchers, watch } from '../lib/watcher.js'
6
6
  import generate from '../lib/generate.js'
7
7
  import path from 'path'
@@ -27,7 +27,7 @@ watchers.push({
27
27
  event: ['change', 'unlink', 'add']
28
28
  })
29
29
  watchers.push({
30
- paths: [watchDir.serverDir, watchDir.helperDir, libDir],
30
+ paths: [watchDir.serverDir, watchDir.helperDir, libDir, packageDir, packageDirCore],
31
31
  callback: () => {
32
32
  childProcess.kill('SIGINT')
33
33
  childProcess = proceed()
@@ -61,3 +61,7 @@ const proceed = () => {
61
61
  }
62
62
 
63
63
  childProcess = proceed()
64
+
65
+ process.on('exit', () => {
66
+ childProcess.kill('SIGINT')
67
+ })
package/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import { allData } from './lib/indexer.js'
2
+ import config from './lib/config.js'
3
+
4
+ export { allData, config }
package/lib/config.js CHANGED
@@ -9,7 +9,7 @@ const config = {
9
9
  "distribute_raw": "image",
10
10
  "relative_path": "",
11
11
  "helper": "",
12
- "package": ""
12
+ "packages": ""
13
13
  }
14
14
  try {
15
15
  const file = rootDir + '/blog.json'
package/lib/dir.js CHANGED
@@ -58,20 +58,52 @@ const cache = () => {
58
58
  // generate の中でもコールしているが、同一プロセスであれば alreadyCached 変数で制御される
59
59
  cache()
60
60
 
61
+ let packageDirectoriesLoaded = []
62
+ const packageDirectories = () => {
63
+ if (packageDirectoriesLoaded.length > 0) {
64
+ return packageDirectoriesLoaded
65
+ }
66
+ const watchTargetDir = ['pages', 'template', 'css', 'helper', 'server', 'js']
67
+ const packages = config.packages.split(',').reduce((prev, packageName) => {
68
+ if (fs.existsSync(`${packageDir}/${packageName}`)) {
69
+ prev.push(fs.realpathSync(`${packageDir}/${packageName}`))
70
+ }
71
+ prev.push(fs.realpathSync(`${packageDirCore}/${packageName}`))
72
+ return prev
73
+ }, [srcDir])
74
+
75
+ for (const baseDir of packages) {
76
+ for (const target of watchTargetDir) {
77
+ if (!fs.existsSync(`${baseDir}/${target}`)) {
78
+ continue;
79
+ }
80
+ const targetDir = `${baseDir}/${target}`
81
+ packageDirectoriesLoaded.push(targetDir)
82
+ }
83
+ }
84
+ return packageDirectoriesLoaded
85
+ }
86
+
61
87
  const resolveDestinationPath = (path) => {
62
- for (const key of Object.keys(watch)) {
63
- if (path.startsWith(watch[key])) {
64
- const dir = watch[key].split('/').pop()
65
- const srcFile = `${dir}${path.replace(watch[key], '')}`
88
+ for (const targetDir of packageDirectories()) {
89
+ if (path.startsWith(targetDir)) {
90
+ const dir = targetDir.split('/').pop()
91
+ const srcFile = `${dir}${path.replace(targetDir, '')}`
66
92
  return `${cacheDir}/${srcFile}`
67
93
  }
68
94
  }
69
95
  }
70
96
  watchers.push({
71
- paths: [srcDir],
97
+ paths: [srcDir, packageDir, packageDirCore],
72
98
  event: ['change', 'add', 'unlink'],
73
99
  callback: ( path ) => {
74
100
  const dest = resolveDestinationPath(path)
101
+ if (!dest) {
102
+ console.log(styleText('red', `[watcher] cannot resolve destination path: ${path}`))
103
+ alreadyCached = false
104
+ cache()
105
+ return true
106
+ }
75
107
  if (fs.existsSync(path)) {
76
108
  fs.cpSync(path, dest, { force: true })
77
109
  console.log(styleText('blue', `update ${path} => ${dest}`))
@@ -95,5 +127,7 @@ export {
95
127
  serverDir,
96
128
  helperDir,
97
129
  watch,
98
- cache
130
+ cache,
131
+ packageDir,
132
+ packageDirCore
99
133
  }
package/lib/server.js CHANGED
@@ -35,8 +35,9 @@ const server = () => {
35
35
  return http.createServer(async (request, response) => {
36
36
  request.setEncoding('utf8')
37
37
  const url = new URL(`http://${request.headers.host}${request.url}`)
38
- const isIndex = url.pathname.match(/(.+)?\/$/)
39
- let path = isIndex ? `${url.pathname}index.html` : decodeURIComponent(url.pathname)
38
+ const urlPathNameDecoded = decodeURIComponent(url.pathname)
39
+ const isIndex = urlPathNameDecoded.match(/(.+)?\/$/)
40
+ let path = isIndex ? `${urlPathNameDecoded}index.html` : urlPathNameDecoded
40
41
  if (!path.includes('.')) {
41
42
  const result = await handle(path, request, response)
42
43
  if (result) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tenjuu99/blog",
3
- "version": "0.2.15",
3
+ "version": "0.2.17",
4
4
  "description": "blog template",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,5 +1,5 @@
1
1
  import { IncomingMessage, ServerResponse } from 'http'
2
- import fs from 'node:fs/promises'
2
+ import fs from 'node:fs'
3
3
  import { styleText } from 'node:util'
4
4
  import config from '@tenjuu99/blog/lib/config.js'
5
5
  import { watch } from '@tenjuu99/blog/lib/dir.js'
@@ -24,7 +24,13 @@ export const post = async (req, res) => {
24
24
  }))
25
25
  return
26
26
  }
27
- await fs.writeFile(`${watch.pageDir}/${file}`, json.content)
27
+ const filenameSplitted = file.split('/')
28
+ const filename = filenameSplitted.pop()
29
+ const dir = [watch.pageDir, ...filenameSplitted].join('/')
30
+ if (!fs.existsSync(dir)) {
31
+ fs.mkdirSync(dir, { recursive: true })
32
+ }
33
+ fs.writeFileSync(`${dir}/${filename}`, json.content)
28
34
  console.log(styleText('blue', '[editor/post] finished'))
29
35
 
30
36
  const href = file.split('.')[0]
@@ -58,9 +58,6 @@ header a, header a:visited {
58
58
 
59
59
  main {
60
60
  flex-grow: 1;
61
- display: flex;
62
- flex-direction: column;
63
- justify-content: space-between;
64
61
  }
65
62
 
66
63
  footer {
@@ -1,6 +1,4 @@
1
- import { allData } from '@tenjuu99/blog/lib/indexer.js'
2
- import replaceVariablesFilter from '@tenjuu99/blog/lib/replaceVariablesFilter.js'
3
- import config from '@tenjuu99/blog/lib/config.js'
1
+ import { allData, config } from '@tenjuu99/blog'
4
2
 
5
3
  export function readIndex (filter = null) {
6
4
  const data = Object.entries(allData)
@@ -15,44 +13,10 @@ export function dateFormat(dateString) {
15
13
  return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`
16
14
  }
17
15
 
18
- export function render(text, variables) {
19
- return replaceVariablesFilter(text, variables)
20
- }
21
-
22
16
  export function getPageData(name) {
23
17
  return allData[name]
24
18
  }
25
19
 
26
- let indexedItemsSorted = null
27
- export function indexedItems() {
28
- if (indexedItemsSorted) {
29
- return indexedItemsSorted
30
- }
31
- const sorted = readIndex()
32
- .filter(v => v.index && v.published != '1970-01-01')
33
- .sort((a, b) => new Date(a.published) - new Date(b.published))
34
- let prev, next
35
- for (const item of sorted) {
36
- if (prev) {
37
- prev.next = {
38
- name: item.name,
39
- published: item.published,
40
- url: item.url,
41
- title: item.title,
42
- }
43
- item.prev = {
44
- name: prev.name,
45
- published: prev.published,
46
- url: prev.url,
47
- title: prev.title,
48
- }
49
- }
50
- prev = item
51
- }
52
- indexedItemsSorted = sorted
53
- return indexedItemsSorted
54
- }
55
-
56
20
  /**
57
21
  * 配列を再帰的に順不同リストに変換する
58
22
  * @param {Array|string} arrayOrText
@@ -118,3 +82,21 @@ export function renderIndex(pages, nodate = 'nodate', headingTag = 'h3') {
118
82
  export function isEditorEnabled() {
119
83
  return allData.editor && allData.editor.distribute
120
84
  }
85
+
86
+ export function breadcrumbList(pageName) {
87
+ const pageData = allData[pageName]
88
+ const breadCrumbs = ['/']
89
+ pageData.url.split('/').reduce((prev, curr) => {
90
+ breadCrumbs.push([`/${prev}${curr}/`, curr])
91
+ return `${prev}${curr}/`
92
+ })
93
+ const last = breadCrumbs.pop()
94
+ last[0] = last[0].substring(0, last[0].length - 1)
95
+
96
+ const output = breadCrumbs.map(v => {
97
+ return `<div style="margin-left: 10px;"><a href="${v[0]}">${v[0] === '/' ? 'top' : v[1]}</a> > </div>`
98
+ }).join('') + `<div style="margin-left: 10px;">${last[1]}</div>`
99
+ return '<div style="display: flex;flex-wrap: wrap;">'
100
+ + output
101
+ + '</div>'
102
+ }
@@ -28,6 +28,9 @@
28
28
  <p class="container"><a href="{{RELATIVE_PATH}}/">{{SITE_NAME}}</a></p>
29
29
  </header>
30
30
  <main>
31
+ <div class="container">
32
+ {{ breadcrumbList(name) }}
33
+ </div>
31
34
  <article class="container">
32
35
  <h1>{{TITLE}}</h1>
33
36
  {if published != '1970-01-01'}