@nera-static/core 4.14.0 → 4.15.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.
- package/package.json +1 -4
- package/src/core.js +48 -5
- package/src/render.js +58 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nera-static/core",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.15.1",
|
|
4
4
|
"description": "The Nera static site generator engine — the build pipeline and the layered theme/view resolver, importable by the Nera CLI, @nera-static/validate and tooling",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -61,12 +61,9 @@
|
|
|
61
61
|
},
|
|
62
62
|
"homepage": "https://github.com/seebaermichi/nera#readme",
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"cpy": "^13.2.2",
|
|
65
64
|
"dotenv": "^18.0.4",
|
|
66
65
|
"fs-readdir-recursive": "^1.1.0",
|
|
67
|
-
"gray-matter": "^4.0.3",
|
|
68
66
|
"markdown-it": "^15.0.0",
|
|
69
|
-
"markdown-it-meta": "^0.0.1",
|
|
70
67
|
"pretty": "^2.0.0",
|
|
71
68
|
"pug": "^3.0.2",
|
|
72
69
|
"rimraf": "^6.0.1",
|
package/src/core.js
CHANGED
|
@@ -1,11 +1,52 @@
|
|
|
1
1
|
import fs from 'fs'
|
|
2
2
|
import path from 'path'
|
|
3
3
|
import fsReaddirRecursive from 'fs-readdir-recursive'
|
|
4
|
-
import meta from 'markdown-it-meta'
|
|
5
4
|
import MarkdownIt from 'markdown-it'
|
|
6
5
|
import yaml from 'yaml'
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
// Frontmatter is parsed with `yaml` like every other YAML file Nera reads, set
|
|
8
|
+
// up to match what pages saw under markdown-it-meta/js-yaml 3: unquoted dates
|
|
9
|
+
// become Date objects, `<<` merge keys work, and a repeated key takes the last
|
|
10
|
+
// value. `nera validate` parses the same block, so a page it accepts parses
|
|
11
|
+
// here too.
|
|
12
|
+
const FRONTMATTER_OPTIONS = {
|
|
13
|
+
schema: 'core',
|
|
14
|
+
customTags: ['timestamp'],
|
|
15
|
+
merge: true,
|
|
16
|
+
uniqueKeys: false,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Block rule for a `---` fenced YAML block on the page's first line, replacing
|
|
20
|
+
// markdown-it-meta. The result goes into the per-render `env`, not onto the
|
|
21
|
+
// shared `md` instance: markdown-it-meta's `md.meta` was never reset, so a page
|
|
22
|
+
// without frontmatter inherited the previous page's.
|
|
23
|
+
const frontmatterRule = (state, start, end) => {
|
|
24
|
+
if (start !== 0 || state.blkIndent !== 0 || state.tShift[start] < 0) {
|
|
25
|
+
return false
|
|
26
|
+
}
|
|
27
|
+
const lineAt = (line) =>
|
|
28
|
+
state.src.slice(state.bMarks[line], state.eMarks[line])
|
|
29
|
+
if (lineAt(start) !== '---') {
|
|
30
|
+
return false
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const data = []
|
|
34
|
+
let line = start
|
|
35
|
+
while (line < end) {
|
|
36
|
+
line++
|
|
37
|
+
if (lineAt(line) === '---' || state.tShift[line] < 0) {
|
|
38
|
+
break
|
|
39
|
+
}
|
|
40
|
+
data.push(lineAt(line))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
state.env.meta = yaml.parse(data.join('\n'), FRONTMATTER_OPTIONS) || {}
|
|
44
|
+
state.line = line + 1
|
|
45
|
+
return true
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const md = new MarkdownIt({ html: true })
|
|
49
|
+
md.block.ruler.before('code', 'frontmatter', frontmatterRule, { alt: [] })
|
|
9
50
|
|
|
10
51
|
// Normalise a site's `base_path` (config/app.yaml) into a URL prefix.
|
|
11
52
|
// Absent/blank/`/` → '' so a root-served site is untouched and renders
|
|
@@ -152,7 +193,9 @@ export const getPagesData = (
|
|
|
152
193
|
|
|
153
194
|
// Read and parse markdown
|
|
154
195
|
const fileContent = fs.readFileSync(fullPath, 'utf-8')
|
|
155
|
-
const
|
|
196
|
+
const env = {}
|
|
197
|
+
const content = md.render(fileContent, env)
|
|
198
|
+
const pageMeta = env.meta || {}
|
|
156
199
|
|
|
157
200
|
// Resolve the creation date. Prefer an author/platform-supplied
|
|
158
201
|
// date from frontmatter — `createdAt`, else `date` — because the
|
|
@@ -163,7 +206,7 @@ export const getPagesData = (
|
|
|
163
206
|
// frontmatter carries neither key do we fall back to `birthtime`,
|
|
164
207
|
// which keeps a purely-local build byte-identical to before.
|
|
165
208
|
// See nera-platform R1 (plans/01) and ROADMAP notes.
|
|
166
|
-
let createdAt =
|
|
209
|
+
let createdAt = pageMeta.createdAt || pageMeta.date
|
|
167
210
|
if (!createdAt) {
|
|
168
211
|
try {
|
|
169
212
|
createdAt = fs.statSync(fullPath).birthtime
|
|
@@ -196,7 +239,7 @@ export const getPagesData = (
|
|
|
196
239
|
results.push({
|
|
197
240
|
content,
|
|
198
241
|
meta: {
|
|
199
|
-
...
|
|
242
|
+
...pageMeta,
|
|
200
243
|
createdAt,
|
|
201
244
|
href: wholeFilePathString,
|
|
202
245
|
fullPath: wholeFilePathString,
|
package/src/render.js
CHANGED
|
@@ -2,14 +2,15 @@ import fs from 'fs/promises'
|
|
|
2
2
|
import fssync from 'fs'
|
|
3
3
|
import path from 'path'
|
|
4
4
|
import { createHash } from 'crypto'
|
|
5
|
-
import cpy from 'cpy'
|
|
6
5
|
import pug from 'pug'
|
|
7
6
|
import pretty from 'pretty'
|
|
8
7
|
import { rimraf } from 'rimraf'
|
|
9
8
|
import dotenv from 'dotenv'
|
|
10
9
|
import { makeLayeredResolver, resolveEntry } from './resolve.js'
|
|
11
10
|
|
|
12
|
-
dotenv
|
|
11
|
+
// `quiet` drops dotenv's `◇ injected env (N) from .env` banner from every build;
|
|
12
|
+
// the `.env` values are still loaded into `process.env`.
|
|
13
|
+
dotenv.config({ quiet: true })
|
|
13
14
|
|
|
14
15
|
const SUCCESS_COLOR = '\x1b[32m%s\x1b[0m'
|
|
15
16
|
|
|
@@ -335,6 +336,47 @@ export function ignoreFiles(ignoreList, filePath, sourceRoot) {
|
|
|
335
336
|
)
|
|
336
337
|
}
|
|
337
338
|
|
|
339
|
+
// OS and editor clutter that never belongs in public/, matched against a file's
|
|
340
|
+
// basename. The list cpy (via the `junk` package) skipped by default; kept so
|
|
341
|
+
// replacing cpy changes nothing a site ships.
|
|
342
|
+
const JUNK =
|
|
343
|
+
/^npm-debug\.log$|^\..*\.swp$|^\.DS_Store$|^\.AppleDouble$|^\.LSOverride$|^Icon\r$|^\._.*|^\.Spotlight-V100(?:$|\/)|\.Trashes|^__MACOSX$|~$|^Thumbs\.db$|^ehthumbs\.db$|^[Dd]esktop\.ini$|@eaDir$/
|
|
344
|
+
|
|
345
|
+
// Every file under `dir`, dotfiles included, junk excluded. Symlinks are
|
|
346
|
+
// followed like cpy's glob did (a linked directory is walked, a dangling link
|
|
347
|
+
// skipped); `seen` stops a link pointing back up the tree from looping.
|
|
348
|
+
const listFiles = async (dir, seen = new Set()) => {
|
|
349
|
+
const real = await fs.realpath(dir)
|
|
350
|
+
if (seen.has(real)) return []
|
|
351
|
+
seen.add(real)
|
|
352
|
+
|
|
353
|
+
const files = []
|
|
354
|
+
for (const entry of await fs.readdir(dir, { withFileTypes: true })) {
|
|
355
|
+
const full = path.join(dir, entry.name)
|
|
356
|
+
let stat = entry
|
|
357
|
+
if (entry.isSymbolicLink()) {
|
|
358
|
+
stat = await fs.stat(full).catch(() => null)
|
|
359
|
+
if (!stat) continue
|
|
360
|
+
}
|
|
361
|
+
if (stat.isDirectory()) {
|
|
362
|
+
files.push(...(await listFiles(full, seen)))
|
|
363
|
+
} else if (stat.isFile() && !JUNK.test(entry.name)) {
|
|
364
|
+
files.push(full)
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return files
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// Copy one file, creating its directory and keeping its mode and timestamps,
|
|
371
|
+
// as cpy's copy-file did.
|
|
372
|
+
const copyPreserving = async (from, to) => {
|
|
373
|
+
await fs.mkdir(path.dirname(to), { recursive: true })
|
|
374
|
+
await fs.copyFile(from, to)
|
|
375
|
+
const { atime, mtime, mode } = await fs.stat(from)
|
|
376
|
+
await fs.utimes(to, atime, mtime)
|
|
377
|
+
await fs.chmod(to, mode)
|
|
378
|
+
}
|
|
379
|
+
|
|
338
380
|
// `ignoreBase` is the directory to read `.neraignore` from:
|
|
339
381
|
// - omitted → the source's parent (back-compat with the pre-theme layout,
|
|
340
382
|
// where `assets/` sat at the site root so its parent *was* the root)
|
|
@@ -349,13 +391,13 @@ export const copyFolder = async (sourceFolder, targetFolder, ignoreBase) => {
|
|
|
349
391
|
const ignore = base === null ? [] : getIgnoredFiles(base)
|
|
350
392
|
|
|
351
393
|
try {
|
|
352
|
-
await
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
}
|
|
394
|
+
for (const file of await listFiles(sourceFolder)) {
|
|
395
|
+
if (!ignoreFiles(ignore, file, sourceFolder)) continue
|
|
396
|
+
await copyPreserving(
|
|
397
|
+
file,
|
|
398
|
+
path.join(targetFolder, path.relative(sourceFolder, file))
|
|
399
|
+
)
|
|
400
|
+
}
|
|
359
401
|
console.log(SUCCESS_COLOR, 'Assets copied')
|
|
360
402
|
} catch (err) {
|
|
361
403
|
console.error('Copy failed:', err)
|
|
@@ -470,10 +512,13 @@ export const createHtmlFiles = async (
|
|
|
470
512
|
await fs.mkdir(path.dirname(htmlPath), { recursive: true })
|
|
471
513
|
await fs.writeFile(htmlPath, pretty(html), 'utf-8')
|
|
472
514
|
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
515
|
+
// Name the file actually written, as its path under `public/`
|
|
516
|
+
// (`/de/index.html`), so every page gets a distinct line.
|
|
517
|
+
const outputPath = path
|
|
518
|
+
.relative(publicFolder, htmlPath)
|
|
519
|
+
.split(path.sep)
|
|
520
|
+
.join('/')
|
|
521
|
+
console.log(SUCCESS_COLOR, `HTML created: /${outputPath}`)
|
|
477
522
|
}
|
|
478
523
|
}
|
|
479
524
|
} else {
|