@tenjuu99/blog 0.1.10 → 0.2.0
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/dir.js +2 -0
- package/lib/pageData.js +2 -3
- package/lib/server.js +8 -2
- package/lib/tryServer.js +58 -0
- package/package.json +1 -1
- package/src-sample/css/layout.css +1 -0
- package/src-sample/pages/editor.html +3 -0
- package/src-sample/server/editor.js +38 -0
- package/src-sample/server/get_editor_target.js +29 -0
- package/src-sample/server/preview.js +28 -0
- package/src-sample/template/default.html +5 -0
- package/src-sample/template/editor.html +197 -0
package/lib/dir.js
CHANGED
|
@@ -7,6 +7,7 @@ const pageDir = `${srcDir}/pages`
|
|
|
7
7
|
const templateDir = `${srcDir}/template`
|
|
8
8
|
const cssDir = `${srcDir}/css`
|
|
9
9
|
const cacheDir = `${rootDir}/.cache`
|
|
10
|
+
const serverDir = `${srcDir}/server`
|
|
10
11
|
|
|
11
12
|
export {
|
|
12
13
|
rootDir,
|
|
@@ -16,4 +17,5 @@ export {
|
|
|
16
17
|
templateDir,
|
|
17
18
|
cssDir,
|
|
18
19
|
cacheDir,
|
|
20
|
+
serverDir
|
|
19
21
|
}
|
package/lib/pageData.js
CHANGED
|
@@ -10,7 +10,7 @@ const parse = (content, name, ext) => {
|
|
|
10
10
|
const regexp = new RegExp(/^(<!|-)--(?<variables>[\s\S]*?)--(-|>)/)
|
|
11
11
|
const matched = content.match(regexp)
|
|
12
12
|
const markdownReplaced = content.replace(regexp, '')
|
|
13
|
-
const metaDataDefault = {
|
|
13
|
+
const metaDataDefault = Object.assign({
|
|
14
14
|
name,
|
|
15
15
|
title: name,
|
|
16
16
|
url: `/${name}`,
|
|
@@ -22,14 +22,13 @@ const parse = (content, name, ext) => {
|
|
|
22
22
|
lang: 'ja',
|
|
23
23
|
site_name: config.site_name,
|
|
24
24
|
url_base: config.url_base,
|
|
25
|
-
gtag_id: config.gtag_id,
|
|
26
25
|
markdown: markdownReplaced,
|
|
27
26
|
relative_path: config.relative_path || '',
|
|
28
27
|
template: 'default.html',
|
|
29
28
|
ext: 'html',
|
|
30
29
|
__output: `/${name}.html`,
|
|
31
30
|
__filetype: ext,
|
|
32
|
-
}
|
|
31
|
+
}, config)
|
|
33
32
|
if (!matched) {
|
|
34
33
|
return metaDataDefault
|
|
35
34
|
}
|
package/lib/server.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import http from 'http'
|
|
2
2
|
import url from 'url'
|
|
3
3
|
import fs from 'node:fs'
|
|
4
|
-
import { distDir } from './dir.js'
|
|
4
|
+
import { distDir, serverDir } from './dir.js'
|
|
5
5
|
import { styleText } from 'node:util'
|
|
6
|
+
import handle from './tryServer.js'
|
|
6
7
|
|
|
7
8
|
const contentType = (ext) => {
|
|
8
9
|
switch (ext) {
|
|
@@ -31,11 +32,16 @@ const contentType = (ext) => {
|
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
const server = () => {
|
|
34
|
-
return http.createServer((request, response) => {
|
|
35
|
+
return http.createServer(async (request, response) => {
|
|
36
|
+
request.setEncoding('utf8')
|
|
35
37
|
const url = new URL(`http://${request.headers.host}${request.url}`)
|
|
36
38
|
const isIndex = url.pathname.match(/(.+)?\/$/)
|
|
37
39
|
let path = isIndex ? `${url.pathname}index.html` : decodeURIComponent(url.pathname)
|
|
38
40
|
if (!path.includes('.')) {
|
|
41
|
+
const result = await handle(path, request, response)
|
|
42
|
+
if (result) {
|
|
43
|
+
return
|
|
44
|
+
}
|
|
39
45
|
path += '.html'
|
|
40
46
|
}
|
|
41
47
|
if (!fs.existsSync(`${distDir}${path}`)) {
|
package/lib/tryServer.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import http from 'http'
|
|
2
|
+
import { distDir, serverDir } from './dir.js'
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import { styleText } from 'node:util'
|
|
5
|
+
|
|
6
|
+
let handlersAlreadyRegistered = false
|
|
7
|
+
const registeredHandlers = {}
|
|
8
|
+
const handlers = async (path) => {
|
|
9
|
+
if (handlersAlreadyRegistered) {
|
|
10
|
+
return registeredHandlers[path]
|
|
11
|
+
}
|
|
12
|
+
const serverFiles = fs.readdirSync(serverDir)
|
|
13
|
+
const loaded = await Promise.all(serverFiles.map(file => import(`${serverDir}/${file}`)))
|
|
14
|
+
loaded.forEach(s => registeredHandlers[s.path] = s)
|
|
15
|
+
handlersAlreadyRegistered = true
|
|
16
|
+
return registeredHandlers[path]
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {string} path
|
|
21
|
+
* @param {http.IncomingMessage} req
|
|
22
|
+
* @param {http.ServerResponse} res
|
|
23
|
+
*/
|
|
24
|
+
const tryServer = async (path, req, res) => {
|
|
25
|
+
const handler = await handlers(path)
|
|
26
|
+
const method = req.method.toLowerCase()
|
|
27
|
+
if (handler && handler[method]) {
|
|
28
|
+
console.log(styleText('blue', `[server ${method.toUpperCase()} ${path}]`))
|
|
29
|
+
try {
|
|
30
|
+
const response = await handler[method](req, res)
|
|
31
|
+
if (response) {
|
|
32
|
+
return response
|
|
33
|
+
}
|
|
34
|
+
} catch (e) {
|
|
35
|
+
console.log(e)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {string} path
|
|
42
|
+
* @param {http.IncomingMessage} request
|
|
43
|
+
* @param {http.ServerResponse} response
|
|
44
|
+
*/
|
|
45
|
+
const getResponse = async (path , request, response) => {
|
|
46
|
+
const url = new URL(`http://${request.headers.host}${request.url}`)
|
|
47
|
+
const res = await tryServer(url.pathname, request, response)
|
|
48
|
+
if (res) {
|
|
49
|
+
if (res === true) {
|
|
50
|
+
return true
|
|
51
|
+
}
|
|
52
|
+
const { status, contentType, body } = res
|
|
53
|
+
response.writeHead(status, {'content-type': contentType })
|
|
54
|
+
return response.end(body)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export default getResponse
|
package/package.json
CHANGED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'http'
|
|
2
|
+
import fs from 'node:fs/promises'
|
|
3
|
+
import { styleText } from 'node:util'
|
|
4
|
+
|
|
5
|
+
const config = (await import('../../lib/config.js')).default
|
|
6
|
+
const dir = (await import('../../lib/dir.js'))
|
|
7
|
+
|
|
8
|
+
export const path = '/editor'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {IncomingMessage} req
|
|
12
|
+
* @param {ServerResponse} res
|
|
13
|
+
*/
|
|
14
|
+
export const post = async (req, res) => {
|
|
15
|
+
const chunks = []
|
|
16
|
+
req
|
|
17
|
+
.on('data', (chunk) => chunks.push(chunk))
|
|
18
|
+
.on('end', async () => {
|
|
19
|
+
const json = JSON.parse(chunks.join())
|
|
20
|
+
const file = json.inputFileName ? json.inputFileName : json.selectDataFile
|
|
21
|
+
if (!file) {
|
|
22
|
+
res.writeHead(401, { 'content-type': 'application/json' })
|
|
23
|
+
res.end(JSON.stringify({
|
|
24
|
+
'message': 'ファイル名がありません'
|
|
25
|
+
}))
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
await fs.writeFile(`${dir.pageDir}/${file}`, json.content)
|
|
29
|
+
console.log(styleText('blue', '[editor/post] finished'))
|
|
30
|
+
|
|
31
|
+
const href = file.split('.')[0]
|
|
32
|
+
res.writeHead(200, { 'content-type': 'application/json' })
|
|
33
|
+
res.end(JSON.stringify({
|
|
34
|
+
'href': `/${href}`
|
|
35
|
+
}))
|
|
36
|
+
})
|
|
37
|
+
return true
|
|
38
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'http'
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
|
|
4
|
+
const config = (await import('../../lib/config.js')).default
|
|
5
|
+
const pageDir = (await import('../../lib/dir.js')).pageDir
|
|
6
|
+
|
|
7
|
+
export const path = '/get_editor_target'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param {IncomingMessage} req
|
|
11
|
+
* @param {ServerResponse} res
|
|
12
|
+
*/
|
|
13
|
+
export const get = async (req, res) => {
|
|
14
|
+
const url = new URL(`${config.url_base}${req.url}`)
|
|
15
|
+
const target = url.searchParams.get('md')
|
|
16
|
+
if (!target) {
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
const file = `${pageDir}/${target}`
|
|
20
|
+
if (!fs.existsSync(`${file}`)) {
|
|
21
|
+
return false
|
|
22
|
+
}
|
|
23
|
+
const f = fs.readFileSync(`${file}`, 'utf8')
|
|
24
|
+
return {
|
|
25
|
+
status: 200,
|
|
26
|
+
contentType: 'application/json',
|
|
27
|
+
body: JSON.stringify({ content: f, filename: target }),
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'http'
|
|
2
|
+
import { styleText } from 'node:util'
|
|
3
|
+
|
|
4
|
+
const render = (await import('../../lib/render.js')).default
|
|
5
|
+
const makePageData = (await import('../../lib/pageData.js')).default
|
|
6
|
+
|
|
7
|
+
export const path = '/preview'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param {IncomingMessage} req
|
|
11
|
+
* @param {ServerResponse} res
|
|
12
|
+
*/
|
|
13
|
+
export const post = async (req, res) => {
|
|
14
|
+
const chunks = []
|
|
15
|
+
req
|
|
16
|
+
.on('data', (chunk) => chunks.push(chunk))
|
|
17
|
+
.on('end', async () => {
|
|
18
|
+
const json = JSON.parse(chunks.join())
|
|
19
|
+
const filename = json.inputFileName ? json.inputFileName : json.selectDataFile
|
|
20
|
+
const pageData = makePageData(filename, json.content)
|
|
21
|
+
const rendered = await render('default.html', pageData)
|
|
22
|
+
res.writeHead(200, { 'content-type': 'application/json' })
|
|
23
|
+
res.end(JSON.stringify({
|
|
24
|
+
'preview': rendered
|
|
25
|
+
}))
|
|
26
|
+
})
|
|
27
|
+
return true
|
|
28
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="ja">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>{{SITE_NAME}}</title>
|
|
7
|
+
{include('template/css.html')}
|
|
8
|
+
<style>
|
|
9
|
+
body {
|
|
10
|
+
background: #fafafa;
|
|
11
|
+
}
|
|
12
|
+
main {
|
|
13
|
+
width: 90%;
|
|
14
|
+
padding: 15px;
|
|
15
|
+
height: 100%;
|
|
16
|
+
margin: 0 auto;
|
|
17
|
+
}
|
|
18
|
+
.textareaAndPreview {
|
|
19
|
+
display: flex;
|
|
20
|
+
margin: 10px 0;
|
|
21
|
+
border: 1px solid #666;
|
|
22
|
+
border-radius: 5px;
|
|
23
|
+
}
|
|
24
|
+
.textareaAndPreview>* {
|
|
25
|
+
flex-basis: 50%;
|
|
26
|
+
height: 80vh;
|
|
27
|
+
}
|
|
28
|
+
.editor {
|
|
29
|
+
display: flex;
|
|
30
|
+
flex-direction: column;
|
|
31
|
+
padding: 15px 0;
|
|
32
|
+
justify-content:center;
|
|
33
|
+
}
|
|
34
|
+
#editorTextArea {
|
|
35
|
+
resize: none;
|
|
36
|
+
background: #cccccc;
|
|
37
|
+
color: #333;
|
|
38
|
+
padding: 5px;
|
|
39
|
+
border-radius: 5px 0 0 5px;
|
|
40
|
+
border: 0;
|
|
41
|
+
}
|
|
42
|
+
form select {
|
|
43
|
+
padding: 5px;
|
|
44
|
+
margin-top: 5px;
|
|
45
|
+
border-radius: 5px;
|
|
46
|
+
}
|
|
47
|
+
#previewContent {
|
|
48
|
+
flex-basis: 100%;
|
|
49
|
+
height: 100%;
|
|
50
|
+
min-height: 90%;
|
|
51
|
+
display: block;
|
|
52
|
+
width: 100%;
|
|
53
|
+
}
|
|
54
|
+
#previewContent iframe {
|
|
55
|
+
width: 100%;
|
|
56
|
+
height: 100%;
|
|
57
|
+
border: none;
|
|
58
|
+
border-radius: 0 5px 5px 0;
|
|
59
|
+
}
|
|
60
|
+
@media screen and (max-width: 600px) {
|
|
61
|
+
main {
|
|
62
|
+
width: 100%;
|
|
63
|
+
display: block;
|
|
64
|
+
}
|
|
65
|
+
.editor {
|
|
66
|
+
padding: 15px 0;
|
|
67
|
+
}
|
|
68
|
+
.preview {
|
|
69
|
+
display: none;
|
|
70
|
+
}
|
|
71
|
+
#editorTextArea {
|
|
72
|
+
flex-basis: 100%;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
</style>
|
|
76
|
+
|
|
77
|
+
<link href="https://cdn.jsdelivr.net/npm/prismjs@v1.x/themes/prism.css" rel="stylesheet" />
|
|
78
|
+
|
|
79
|
+
<script src="https://cdn.jsdelivr.net/npm/prismjs@v1.x/components/prism-core.min.js"></script>
|
|
80
|
+
<script src="https://cdn.jsdelivr.net/npm/prismjs@v1.x/plugins/autoloader/prism-autoloader.min.js"></script>
|
|
81
|
+
</head>
|
|
82
|
+
<body>
|
|
83
|
+
<script>
|
|
84
|
+
const sleep = waitTime => new Promise( resolve => setTimeout(resolve, waitTime) );
|
|
85
|
+
|
|
86
|
+
const fetchData = (target) => {
|
|
87
|
+
return fetch(`/get_editor_target?md=${target}`)
|
|
88
|
+
.then(async res => {
|
|
89
|
+
const json = await res.json()
|
|
90
|
+
return json
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
const onloadFunction = async (e) => {
|
|
94
|
+
const form = document.querySelector('#editor')
|
|
95
|
+
const textarea = form.querySelector('#editorTextArea')
|
|
96
|
+
const select = form.querySelector('#selectDataFile')
|
|
97
|
+
const inputFileName = form.querySelector('#inputFileName')
|
|
98
|
+
const preview = document.querySelector('#previewContent')
|
|
99
|
+
const url = new URL(location)
|
|
100
|
+
const target = url.searchParams.get('md')
|
|
101
|
+
if (target) {
|
|
102
|
+
fetchData(target).then(json => {
|
|
103
|
+
textarea.value = json.content
|
|
104
|
+
select.value = json.filename
|
|
105
|
+
inputFileName.value = json.filename
|
|
106
|
+
inputFileName.setAttribute('disabled', true)
|
|
107
|
+
submit('/preview', form)
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
select.addEventListener('change', async (event) => {
|
|
111
|
+
if (select.value) {
|
|
112
|
+
const json = await fetchData(select.value)
|
|
113
|
+
textarea.value = json.content
|
|
114
|
+
inputFileName.value = json.filename
|
|
115
|
+
inputFileName.setAttribute('disabled', true)
|
|
116
|
+
url.searchParams.set('md', select.value)
|
|
117
|
+
} else {
|
|
118
|
+
inputFileName.value = ""
|
|
119
|
+
inputFileName.removeAttribute('disabled')
|
|
120
|
+
}
|
|
121
|
+
history.pushState({}, "", url)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
const submit = (fetchUrl, form) => {
|
|
125
|
+
const formData = new FormData(form)
|
|
126
|
+
const obj = {}
|
|
127
|
+
formData.forEach((v, k) => {
|
|
128
|
+
obj[k] = v
|
|
129
|
+
})
|
|
130
|
+
fetch(fetchUrl, {
|
|
131
|
+
method: 'post',
|
|
132
|
+
body: JSON.stringify(obj)
|
|
133
|
+
}).then(async response => {
|
|
134
|
+
const json = await response.json()
|
|
135
|
+
if (!response.ok) {
|
|
136
|
+
alert(json.message)
|
|
137
|
+
return
|
|
138
|
+
}
|
|
139
|
+
if (json.href) {
|
|
140
|
+
await sleep(300)
|
|
141
|
+
location.href = json.href
|
|
142
|
+
}
|
|
143
|
+
if (json.preview) {
|
|
144
|
+
const iframe = document.createElement('iframe')
|
|
145
|
+
iframe.setAttribute('srcdoc', json.preview)
|
|
146
|
+
const old = preview.querySelector('iframe')
|
|
147
|
+
if (!old) {
|
|
148
|
+
preview.appendChild(iframe)
|
|
149
|
+
}
|
|
150
|
+
old.setAttribute('srcdoc', json.preview)
|
|
151
|
+
}
|
|
152
|
+
}).catch(e => {
|
|
153
|
+
console.log(e.message)
|
|
154
|
+
})
|
|
155
|
+
}
|
|
156
|
+
form.addEventListener('submit', (event) => {
|
|
157
|
+
event.preventDefault()
|
|
158
|
+
const fetchUrl = event.submitter.dataset.url
|
|
159
|
+
submit(fetchUrl, event.target)
|
|
160
|
+
})
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
document.addEventListener('DOMContentLoaded', (event) => {
|
|
164
|
+
onloadFunction(event)
|
|
165
|
+
})
|
|
166
|
+
</script>
|
|
167
|
+
<main>
|
|
168
|
+
<form action="/editor" class="editor" method="post" id="editor">
|
|
169
|
+
<input id="inputFileName" name="inputFileName" type="text" value="" placeholder="sample.md">
|
|
170
|
+
<select id="selectDataFile" name="selectDataFile">
|
|
171
|
+
<option value="">新規作成</option>
|
|
172
|
+
<script type="ssg">
|
|
173
|
+
return helper.readIndex().map(p => {
|
|
174
|
+
return `<option value="${p.name}.${p.__filetype}">${p.url}.${p.__filetype}</option>`
|
|
175
|
+
}).join("\n")
|
|
176
|
+
</script>
|
|
177
|
+
</select>
|
|
178
|
+
<div class="textareaAndPreview">
|
|
179
|
+
<textarea id="editorTextArea" name="content" cols="30" rows="10">
|
|
180
|
+
# H1
|
|
181
|
+
|
|
182
|
+
ここにマークダウンを入力してください。
|
|
183
|
+
</textarea>
|
|
184
|
+
<div class="preview">
|
|
185
|
+
<div id="previewContent"></div>
|
|
186
|
+
</div>
|
|
187
|
+
</div>
|
|
188
|
+
<input type="hidden" name="token" value="{{TOKEN}}">
|
|
189
|
+
<div>
|
|
190
|
+
<input type="submit" value="preview" data-url="/preview">
|
|
191
|
+
<input type="submit" value="編集" data-url="/editor">
|
|
192
|
+
<a href="/">戻る</a>
|
|
193
|
+
</div>
|
|
194
|
+
</form>
|
|
195
|
+
</main>
|
|
196
|
+
</body>
|
|
197
|
+
</html>
|