Skip to content

Commit

Permalink
feat: (WIP) load marked extensions from js file
Browse files Browse the repository at this point in the history
  • Loading branch information
nobkd committed Mar 14, 2024
1 parent 217c5f5 commit 44aea5d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
4 changes: 2 additions & 2 deletions packages/nuekit/src/nuekit.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function createKit(args) {
// site: various file based functions
const site = await createSite(args)

const { dist, port, read, copy, write, is_empty } = site
const { dist, port, read, copy, write, is_empty, marked_extensions } = site
const is_dev = !is_prod

// make sure @nue dir has all the latest
Expand Down Expand Up @@ -134,7 +134,7 @@ export async function createKit(args) {
const dir = data.appdir || file.dir
const lib = await site.getLayoutComponents(dir)

data.content = renderPage(data.page, { data, lib }).html
data.content = renderPage(data.page, { data, lib, marked_extensions }).html

function render(name, def) {
const layout = lib.find(el => el.tagName == name) || def && parseNue(def)[0]
Expand Down
6 changes: 5 additions & 1 deletion packages/nuekit/src/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export async function createSite(args) {
self.is_empty = true
}

const markedConfig = join(process.cwd(), root, site_data.marked_config || 'marked.config.js')
const { default: marked_extensions=[] } = await import(markedConfig).catch(() => ({}))
// TODO: exclude `markedConfig` path from output dir

async function write(content, dir, filename) {
const todir = join(dist, dir)

Expand Down Expand Up @@ -300,6 +304,6 @@ export async function createSite(args) {
}
}

return { ...self, dist, port, read, write, copy }
return { ...self, dist, port, read, write, copy, marked_extensions }

}
65 changes: 35 additions & 30 deletions packages/nuemark/src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,48 @@ import { parsePage, parseHeading } from './parse.js'
import { parseAttr } from './component.js'
import { marked } from 'marked'

let exts_loaded = false

marked.setOptions({
smartypants: true,
headerIds: false,
smartLists: false,
mangle: false
})

// marked renderers
const renderer = {

heading(html, level, raw) {
const plain = parseHeading(raw)
const cls = plain.class
const title = plain.text.replaceAll('"', '')
const rich = parseHeading(html)

delete plain.text
const a = elem('a', { href: `#${plain.id}`, title: title })
return elem(`h${level}`, plain, a + rich.text)
},

// lazyload images by default
image(src, title, alt) {
return elem('img', { src, title, alt, loading: 'lazy' })
},
}

marked.use({ renderer })

export function renderPage(page, opts) {
const { lib=[] } = opts
const { lib=[], marked_extensions=[] } = opts
const data = { ...opts.data, ...page.meta }
const draw_sections = data?.draw_sections || page.sections[1]
const section_attr = data.sections || []
const ret = []

if (!exts_loaded && marked_extensions.length) {
exts_loaded = true
marked.use(...marked_extensions)
}

// section_attr
page.sections.forEach((section, i) => {
Expand Down Expand Up @@ -94,32 +128,3 @@ function parseLink(href) {
if (title) href = href.slice(0, i)
return { href, title }
}

marked.setOptions({
smartypants: true,
headerIds: false,
smartLists: false,
mangle: false
})

// marked renderers
const renderer = {

heading(html, level, raw) {
const plain = parseHeading(raw)
const cls = plain.class
const title = plain.text.replaceAll('"', '')
const rich = parseHeading(html)

delete plain.text
const a = elem('a', { href: `#${plain.id}`, title: title })
return elem(`h${level}`, plain, a + rich.text)
},

// lazyload images by default
image(src, title, alt) {
return elem('img', { src, title, alt, loading: 'lazy' })
},
}

marked.use({ renderer })

0 comments on commit 44aea5d

Please sign in to comment.