@tenjuu99/blog 0.2.25 → 0.2.27

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/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { allData } from './lib/indexer.js'
2
2
  import config from './lib/config.js'
3
+ import * as dir from './lib/dir.js'
3
4
 
4
- export { allData, config }
5
+ export { allData, config, dir }
package/lib/dir.js CHANGED
@@ -36,10 +36,16 @@ const cache = () => {
36
36
  const packages = config.packages.split(',')
37
37
  packages.forEach(dir => {
38
38
  if (fs.existsSync(`${packageDirCore}/${dir}`)) {
39
+ const helper = config.helper.split(',')
40
+ helper.push(`${dir}.js`)
41
+ config.helper = helper.join(',')
39
42
  console.log(styleText('blue', `[cache] enable core package: ${dir}`))
40
43
  fs.cpSync(`${packageDirCore}/${dir}`, cacheDir, { recursive: true })
41
44
  }
42
45
  if (fs.existsSync(`${packageDir}/${dir}`)) {
46
+ const helper = config.helper.split(',')
47
+ helper.push(`${dir}.js`)
48
+ config.helper = helper.join(',')
43
49
  console.log(styleText('blue', `[cache] enable package: ${dir}`))
44
50
  fs.cpSync(`${packageDir}/${dir}`, cacheDir, { recursive: true })
45
51
  }
package/lib/helper.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import config from './config.js'
2
+ import { existsSync } from 'node:fs'
2
3
  import { helperDir } from './dir.js'
3
4
 
4
5
  let helper = {}
@@ -6,8 +7,10 @@ let helper = {}
6
7
  if (config.helper) {
7
8
  const files = config.helper.split(',')
8
9
  files.forEach(async file => {
9
- const helperAdditional = await import(`${helperDir}/${file}`)
10
- helper = Object.assign(helper, helperAdditional)
10
+ if (existsSync(`${helperDir}/${file}`)) {
11
+ const helperAdditional = await import(`${helperDir}/${file}`)
12
+ helper = Object.assign(helper, helperAdditional)
13
+ }
11
14
  })
12
15
  }
13
16
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tenjuu99/blog",
3
- "version": "0.2.25",
3
+ "version": "0.2.27",
4
4
  "description": "blog template",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,7 @@
1
+ import fs from 'node:fs'
2
+ import { dir } from '@tenjuu99/blog'
3
+
4
+ export function turbolink() {
5
+ const turbolinkScript = fs.readFileSync(dir.templateDir + '/turbolink.js', 'utf8')
6
+ return `<script>${turbolinkScript}</script>`
7
+ }
@@ -31,7 +31,17 @@ const transition = async (href) => {
31
31
  document.body = doc.getElementsByTagName('body')[0]
32
32
  // set header
33
33
  document.title = doc.getElementsByTagName('title')[0].textContent
34
- const canonical = document.head.querySelector('link[rel=canonical]').href = doc.querySelector('link[rel=canonical]').href
34
+ const canonical = document.head.querySelector('link[rel=canonical]')
35
+ const newCanonical = doc.querySelector('link[rel=canonical]')
36
+ if (canonical) {
37
+ if (newCanonical) {
38
+ canonical.href = newCanonical.href
39
+ } else {
40
+ canonical.remove()
41
+ }
42
+ } else if (newCanonical) {
43
+ document.head.appendChild(newCanonical)
44
+ }
35
45
 
36
46
  const metas = [...document.head.querySelectorAll('meta')]
37
47
  metas.forEach(meta => {
@@ -51,15 +61,38 @@ const transition = async (href) => {
51
61
  document.head.appendChild(meta)
52
62
  }
53
63
  })
64
+ const scripts = [...document.scripts].forEach(script => {
65
+ if (script.dataset.turbolink === 'reapply') {
66
+ const newScript = document.createElement('script')
67
+ newScript.dataset.turbolink = 'reapply'
68
+ const newScriptHtml = `(() => {
69
+ ${script.innerHTML}
70
+ })()`
71
+ newScript.innerHTML = newScriptHtml
72
+ script.parentNode.append(newScript)
73
+ script.remove()
74
+ }
75
+ })
54
76
  }
55
77
 
78
+ const urlFromHref = (href) => {
79
+ try {
80
+ return new URL(encodeURI(href))
81
+ } catch (e) {
82
+ console.log(`${href} is invalid`)
83
+ throw e
84
+ }
85
+ }
56
86
  const turbolinks = () => {
57
87
  const links = document.querySelectorAll('a')
58
- const current = new URL(document.location.href)
88
+ const current = urlFromHref(document.location.href)
59
89
  const currentDom = document.body
60
90
  links.forEach(link => {
61
91
  const href = link.href
62
- const url = new URL(href)
92
+ if (!href.trim() || href.startsWith('javascript:') || link.dataset['turbolink'] === 'disable') {
93
+ return
94
+ }
95
+ const url = urlFromHref(href)
63
96
  if (url.host === current.host) {
64
97
  link.onclick = async (e) => {
65
98
  e.preventDefault()
@@ -47,12 +47,6 @@
47
47
 
48
48
  {include('template/prevNext.html')}
49
49
  </main>
50
- {if isEditorEnabled() }
51
- <div class="container">
52
- <a href="/editor?md={{name}}.{{__filetype}}" class="editor_link">編集する</a>
53
- </div>
54
- {/if}
55
-
56
50
  {include('template/footer.html')}
57
51
  {if add_script}
58
52
  <script src="{{ADD_SCRIPT}}" async></script>
@@ -1,13 +1,20 @@
1
- <footer>
2
- <ul class="container">
3
- <li><a href="{{RELATIVE_PATH}}/about">about</a></li>
4
- <li><a href="{{RELATIVE_PATH}}/privacy_policy">プライバシーポリシー</a></li>
5
- <li><a href="{{RELATIVE_PATH}}/rss.xml">RSS</a></li>
6
- </ul>
7
- </footer>
1
+ {if isEditorEnabled() }
2
+ <div class="container">
3
+ <a href="/editor?md={{name}}.{{__filetype}}" class="editor_link" data-turbolink="disable">編集する</a>
4
+ </div>
5
+ {/if}
6
+
7
+ <footer>
8
+ <ul class="container">
9
+ <li><a href="{{RELATIVE_PATH}}/about">about</a></li>
10
+ <li><a href="{{RELATIVE_PATH}}/privacy_policy">プライバシーポリシー</a></li>
11
+ <li><a href="{{RELATIVE_PATH}}/rss.xml" data-turbolink="disable">RSS</a></li>
12
+ </ul>
13
+ </footer>
8
14
 
9
- {if turbolink}
10
- <script>
11
- {include('template/turbolink.js')}
15
+ <script data-turbolink="reapply">
16
+ const title = document.title
17
+ console.log(title)
12
18
  </script>
13
- {/if}
19
+
20
+ {{ turbolink() }}