Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: marked extensions #230

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/nuekit/src/nuefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function fswalk(root, _dir='', _ret=[]) {
return _ret
}

const IGNORE = ['node_modules', 'functions', 'package.json', 'bun.lockb', 'pnpm-lock.yaml', 'README.md']
const IGNORE = ['node_modules', 'functions', 'package.json', 'bun.lockb', 'pnpm-lock.yaml', 'yarn.lock', 'package-lock.json', 'marked.config.js', 'README.md']

function ignore(name='') {
return '._'.includes(name[0]) || IGNORE.includes(name)
Expand Down
10 changes: 9 additions & 1 deletion packages/nuekit/src/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { join, extname, parse as parsePath } from 'node:path'
import { parse as parseNue } from 'nuejs-core'
import { promises as fs, existsSync } from 'node:fs'
import { fswalk } from './nuefs.js'
import { nuemark } from 'nuemark'
import { nuemark, loadMarkedExtensions } from 'nuemark'
import yaml from 'js-yaml'


Expand Down Expand Up @@ -80,6 +80,14 @@ export async function createSite(args) {
// flag if .dist is empty
if (!existsSync(dist)) self.is_empty = true

const markedConfig = joinRootPath(root, 'marked.config.js', true)
const markedConfigExists = await fs.stat(markedConfig).catch(() => false)
const { default: markedExtensions=[] } = markedConfigExists ? await import(markedConfig).catch(e => {
console.error(e)
return {}
}) : {}
loadMarkedExtensions(markedExtensions)

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

Expand Down
21 changes: 21 additions & 0 deletions packages/nuekit/test/nuekit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,24 @@ test('the project was started for the first time', async () => {
terminate()
}
})

test('marked extension config file', async() => {
const marked_config = `export default [{
tokenizer: {
inlineText(src) {
const cap = this.rules.inline.text.exec(src)
const text = cap[0].replace(/\\.{3}/g, '\\u2026')
return { type: 'text', raw: cap[0], text }
}
}
}]`

await write('marked.config.js', marked_config)
await write('index.md', 'This is a test, right?\n\n...\n\nRight?\n\n.....')

const kit = await getKit()
const html = await kit.gen('index.md')

const ellipsis = '…'
expect(html).toInclude(`<p>This is a test, right?</p>\n<p>${ellipsis}</p>\n<p>Right?</p>\n<p>${ellipsis}..</p>\n`)
})
2 changes: 1 addition & 1 deletion packages/nuemark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ export function nuemarkdown(str, opts) {
export { parsePage } from './src/parse.js'


export { renderPage } from './src/render.js'
export { renderPage, loadMarkedExtensions } from './src/render.js'
11 changes: 4 additions & 7 deletions packages/nuemark/src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,6 @@ function parseLink(href) {
return { href, title }
}

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

export function renderHeading(html, depth, raw) {
const plain = parseHeading(raw)
const { id } = plain
Expand Down Expand Up @@ -135,3 +128,7 @@ const renderer = {
}

marked.use({ renderer })

export function loadMarkedExtensions(marked_extensions = []) {
marked.use(...marked_extensions)
}
19 changes: 18 additions & 1 deletion packages/nuemark/test/nuemark.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parseMeta, parseBlocks, parseSections, parsePage, parseHeading } from '../src/parse.js'
import { parseComponent, valueGetter, parseAttr, parseSpecs } from '../src/component.js'
import { renderIsland, renderLines, renderHeading } from '../src/render.js'
import { renderIsland, renderLines, renderHeading, loadMarkedExtensions } from '../src/render.js'
import { tags, parseSize } from '../src/tags.js'
import { nuemarkdown } from '../index.js'

Expand Down Expand Up @@ -442,6 +442,23 @@ test('blockquotes', () => {
expect(html).toInclude('</blockquote>\n<p>joe</p>')
})

test('marked extension', async () => {
const ellipsis = '\u2026'
loadMarkedExtensions([{
tokenizer: {
inlineText(src) {
const cap = this.rules.inline.text.exec(src)
const text = cap[0].replace(/\.{3}/g, ellipsis)
return { type: 'text', raw: cap[0], text }
}
}
}])

const { html } = renderLines(["This isn't a test, right?\n", '...\n', 'Right?\n', '.....'])

expect(html).toBe(`<p>This isn't a test, right?</p>\n<p>${ellipsis}</p>\n<p>Right?</p>\n<p>${ellipsis}..</p>\n`)
})

/*
Required:
bun add react
Expand Down