Skip to content

Commit

Permalink
Cleanup data namespace with data.assets object
Browse files Browse the repository at this point in the history
  • Loading branch information
tipiirai committed Nov 1, 2024
1 parent 43111f0 commit 47ab781
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 43 deletions.
4 changes: 2 additions & 2 deletions packages/nuekit/src/layout/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 2 additions & 4 deletions packages/nuekit/src/layout/head.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [`<meta charset="${charset}">`]
if (title) head.push(elem('title', title_template.replace(/%s/gi, title)))

Expand Down
6 changes: 3 additions & 3 deletions packages/nuekit/src/layout/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

// <main>...</main>
if (!slots.main && data.main !== false) {
Expand Down
31 changes: 17 additions & 14 deletions packages/nuekit/src/nuekit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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) {
Expand All @@ -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')
}

Expand All @@ -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
Expand All @@ -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 })
}


Expand All @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions packages/nuekit/src/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) })
}

Expand All @@ -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 })
Expand Down
4 changes: 2 additions & 2 deletions packages/nuekit/test/layout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ test('renderSlots', () => {
})

test('renderPage', () => {
const doc = { render: () => '<h1>Hello</h1>' }
const document = { render: () => '<h1>Hello</h1>' }
const lib = [{ tagName: 'header', render: () => '<header>' }]
const html = renderPage({ doc, lib, data: { language: 'fi' }})
const html = renderPage({ document, lib, data: { language: 'fi' }})
expect(html).toStartWith('<html lang="fi" dir="ltr">')
expect(html).toInclude('<header></header>')
expect(html).toInclude('<h1>Hello</h1>')
Expand Down
18 changes: 9 additions & 9 deletions packages/nuekit/test/nuekit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
})


Expand All @@ -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" ])
})

Expand Down Expand Up @@ -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('<style href="/inline/style.css">')
expect(html).toInclude('margin:')
Expand Down Expand Up @@ -325,10 +325,10 @@ test('page assets', async () => {
await write('blog/main.js', 'var a')

const kit = await getKit()
const data = await kit.getPageData('blog/index.md')
const { assets } = await kit.getPageData('blog/index.md')

expect(data.components).toEqual(["/blog/comp.js", "/lib/video.js"])
expect(data.scripts.length).toEqual(4)
expect(assets.components).toEqual(["/blog/comp.js", "/lib/video.js"])
expect(assets.scripts.length).toEqual(4)
})


Expand Down
1 change: 0 additions & 1 deletion packages/nuemark/src/parse-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export function parseDocument(lines) {
},

codeblocks: blocks.filter(el => el.is_code),
sections,
meta,
}
}
Expand Down
6 changes: 1 addition & 5 deletions packages/nuemark/test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ test('non section', () => {
expect(sectionize(blocks)).toBeUndefined()
})

test('single section', () => {
const { sections } = parseDocument(['Hello'])
expect(sections.length).toBe(1)
})

test('multiple sections', () => {
const lines = [
Expand All @@ -73,7 +69,7 @@ test('multiple sections', () => {
]

const doc = parseDocument(lines)
expect(doc.sections.length).toBe(2)
// expect(doc.sections.length).toBe(2)

const html = doc.render({ sections: ['hero'] })
expect(html).toStartWith('<section class="hero"><h1>Hello</h1>')
Expand Down

0 comments on commit 47ab781

Please sign in to comment.