From 47ab781b90c195ca8cdb35ee18b10d2511fef1df Mon Sep 17 00:00:00 2001 From: Tero Piirainen Date: Fri, 1 Nov 2024 15:12:52 +0200 Subject: [PATCH] Cleanup data namespace with data.assets object --- packages/nuekit/src/layout/components.js | 4 +-- packages/nuekit/src/layout/head.js | 6 ++--- packages/nuekit/src/layout/page.js | 6 ++--- packages/nuekit/src/nuekit.js | 31 +++++++++++++----------- packages/nuekit/src/site.js | 6 ++--- packages/nuekit/test/layout.test.js | 4 +-- packages/nuekit/test/nuekit.test.js | 18 +++++++------- packages/nuemark/src/parse-document.js | 1 - packages/nuemark/test/document.test.js | 6 +---- 9 files changed, 39 insertions(+), 43 deletions(-) diff --git a/packages/nuekit/src/layout/components.js b/packages/nuekit/src/layout/components.js index 66d6c83c..6cd9574d 100644 --- a/packages/nuekit/src/layout/components.js +++ b/packages/nuekit/src/layout/components.js @@ -25,8 +25,8 @@ export function renderNavi(data) { function renderTOC(data) { - const { doc, attr } = data - return doc.renderTOC(attr) + const { document, attr } = data + return document.renderTOC(attr) } function renderPrettyDate(date) { diff --git a/packages/nuekit/src/layout/head.js b/packages/nuekit/src/layout/head.js index 417c948e..94fe46bd 100644 --- a/packages/nuekit/src/layout/head.js +++ b/packages/nuekit/src/layout/head.js @@ -18,18 +18,16 @@ export function renderHead(data) { viewport = 'width=device-width,initial-scale=1', charset = 'utf-8', title_template = '%s', - scripts = [], - styles = [], - inline_css = [], prefetch = [], base = '', origin = '', - components = [], favicon, title, is_prod, } = data + const { scripts=[], styles=[], inline_css=[], components=[] } = data.assets || {} + const head = [``] if (title) head.push(elem('title', title_template.replace(/%s/gi, title))) diff --git a/packages/nuekit/src/layout/page.js b/packages/nuekit/src/layout/page.js index 42031c1a..9c789ed4 100644 --- a/packages/nuekit/src/layout/page.js +++ b/packages/nuekit/src/layout/page.js @@ -114,19 +114,19 @@ function convertToTags(components, data) { } -export function renderPage({ doc, data, lib }) { +export function renderPage({ document, data, lib }) { const comps = [ ...lib, ...getLayoutComponents()] const slots = renderSlots(data, comps) const tags = { ...convertToTags(lib, data), 'page-list': renderPageList, - toc: doc.renderTOC + toc: document.renderTOC } // nuemark opts: { data, sections, heading_ids, links, tags } const { heading_ids, sections, links } = data - const content = doc.render({ data, heading_ids, sections, links, tags }) + const content = document.render({ data, heading_ids, sections, links, tags }) //
...
if (!slots.main && data.main !== false) { diff --git a/packages/nuekit/src/nuekit.js b/packages/nuekit/src/nuekit.js index 63686fd1..46cfc161 100644 --- a/packages/nuekit/src/nuekit.js +++ b/packages/nuekit/src/nuekit.js @@ -43,14 +43,15 @@ export async function createKit(args) { async function setupStyles(dir, data) { const paths = await site.getStyles(dir, data) + const { assets } = data if (data.inline_css) { - data.inline_css = await buildAllCSS(paths) - data.styles = paths.filter(path => path.includes('@nue')) + assets.inline_css = await buildAllCSS(paths) + assets.styles = paths.filter(path => path.includes('@nue')) } else { - data.inline_css = [] - data.styles = paths + assets.inline_css = [] + assets.styles = paths } } @@ -68,10 +69,11 @@ export async function createKit(args) { async function setupScripts(dir, data) { // scripts - const scripts = data.scripts = await site.getScripts(dir, data) + const { assets } = data + const scripts = assets.scripts = await site.getScripts(dir, data) // components - data.components = await site.getClientComponents(dir, data) + assets.components = await site.getClientComponents(dir, data) // system scripts function push(name) { @@ -80,7 +82,7 @@ export async function createKit(args) { } if (is_dev && data.hotreload !== false) push('hotreload') - if (data.components?.length) push('mount') + if (assets.components?.length) push('mount') if (data.view_transitions || data.router) push('view-transitions') } @@ -89,14 +91,13 @@ export async function createKit(args) { // markdown data: meta, sections, headings, links const raw = await read(path) - const doc = nuedoc(raw) - const { meta } = doc + const document = nuedoc(raw) + const { meta } = document const { dir } = parsePath(path) const data = await site.getData(meta.appdir || dir) - // YAML data - Object.assign(data, parsePathParts(path), { doc }) + // include & exclude concatenation extendData(data, meta) // content collection @@ -108,21 +109,22 @@ export async function createKit(args) { // scripts & styling const asset_dir = meta.appdir || dir + data.assets = {} await setupScripts(asset_dir, data) await setupStyles(asset_dir, data) - return data + return { ...data, ...parsePathParts(path), document } } // Markdown page async function renderMPA(path) { const data = await getPageData(path) - const { doc } = data + const { document } = data const file = parsePath(path) const lib = await site.getServerComponents(data.appdir || file.dir, data) - return DOCTYPE + renderPage({ doc, data, lib }) + return DOCTYPE + renderPage({ document, data, lib }) } @@ -136,6 +138,7 @@ export async function createKit(args) { const data = { ...await site.getData(appdir), ...parsePathParts(index_path) } // scripts & styling + data.assets = {} await setupScripts(dir, data) await setupStyles(dir, data) diff --git a/packages/nuekit/src/site.js b/packages/nuekit/src/site.js index 08180f32..ee81ab8e 100644 --- a/packages/nuekit/src/site.js +++ b/packages/nuekit/src/site.js @@ -243,8 +243,8 @@ export async function createSite(args) { const mds = paths.filter(el => el.endsWith('.md')).map(el => join(dir, el)) for (const path of mds) { - const doc = nuedoc(await read(path)) - const { meta } = doc + const document = nuedoc(await read(path)) + const { meta } = document if (!meta.unlisted) arr.push({ ...meta, ...parsePathParts(path) }) } @@ -262,7 +262,7 @@ export async function createSite(args) { let paths = await getAssets({ dir, exts: ['css'], data }) // syntax highlighting - if (data.syntax_highlight !== false && data.doc?.codeblocks[0]) paths.push(`/@nue/syntax.css`) + if (data.syntax_highlight !== false && data.document?.codeblocks[0]) paths.push(`/@nue/syntax.css`) // cascading order: globals -> area -> page sortCSS({ paths, globals: self.globals, dir }) diff --git a/packages/nuekit/test/layout.test.js b/packages/nuekit/test/layout.test.js index 5d97fc14..31fd5478 100644 --- a/packages/nuekit/test/layout.test.js +++ b/packages/nuekit/test/layout.test.js @@ -42,9 +42,9 @@ test('renderSlots', () => { }) test('renderPage', () => { - const doc = { render: () => '

Hello

' } + const document = { render: () => '

Hello

' } const lib = [{ tagName: 'header', render: () => '
' }] - const html = renderPage({ doc, lib, data: { language: 'fi' }}) + const html = renderPage({ document, lib, data: { language: 'fi' }}) expect(html).toStartWith('') expect(html).toInclude('
') expect(html).toInclude('

Hello

') diff --git a/packages/nuekit/test/nuekit.test.js b/packages/nuekit/test/nuekit.test.js index 262e98ce..ce1b809e 100644 --- a/packages/nuekit/test/nuekit.test.js +++ b/packages/nuekit/test/nuekit.test.js @@ -110,8 +110,8 @@ test('root styles', async () => { await write('globals/bar.css') await write('home.css') await write('index.md') - const { styles } = await kit.getPageData('index.md') - expect(styles).toEqual(["/home.css"]) + const { assets } = await kit.getPageData('index.md') + expect(assets.styles).toEqual(["/home.css"]) }) @@ -136,9 +136,9 @@ test('asset include/exclude', async () => { await write('blog/app.yaml', 'include: [lib]\nexclude: [kama]') const kit = await getKit() - const data = await kit.getPageData('blog/index.md') + const { assets } = await kit.getPageData('blog/index.md') - expect(data.styles).toEqual(["/global/global.css", "/lib/zoo.css"]) + expect(assets.styles).toEqual(["/global/global.css", "/lib/zoo.css"]) // expect(data.components).toEqual([ "/global/kama.js", "/lib/zoo.css" ]) }) @@ -296,8 +296,8 @@ test('inline CSS', async () => { await write('inline/style.css', 'body { margin: 0 }') await write('inline/app.yaml', 'inline_css: true') await write('inline/index.md', '# Hey') - const data = await kit.getPageData('inline/index.md') - expect(data.inline_css[0].path).toEqual('/inline/style.css') + const { assets } = await kit.getPageData('inline/index.md') + expect(assets.inline_css[0].path).toEqual('/inline/style.css') const html = await kit.gen('inline/index.md') expect(html).toInclude('