Skip to content

Commit

Permalink
fix: unnamed block tag now defaults to div, don't skip list issue test
Browse files Browse the repository at this point in the history
  • Loading branch information
nobkd committed Feb 4, 2025
1 parent a99e432 commit d658399
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
6 changes: 4 additions & 2 deletions packages/nuemark/src/parse-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export function parseBlocks(lines, capture) {
// tag
if (c == '[' && trimmed.endsWith(']') && !trimmed.includes('][')) {
const tag = parseTag(line.slice(1, -1))
block = { is_tag: true, ...tag, body: [] }
block = { is_tag: true, ...tag, name: tag.name || 'div', body: [] }
return blocks.push(block)
}

Expand Down Expand Up @@ -182,7 +182,9 @@ function processNestedBlocks(block, capture) {
const body = block.body.join('\n')

try {
if (body && name && isYAML(body.trim())) {
// TODO: add additional check for native html tags
// maybe new syntax? e.g. `[yaml-tag]:\n\thi` (note colon)
if (body && isYAML(body.trim())) {
let data = parseYAML(body)
if (Array.isArray(data)) data = { items: data }
Object.assign(block.data, data)
Expand Down
14 changes: 6 additions & 8 deletions packages/nuemark/src/render-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ const TAGS = {
const html = !divs || !divs[1] ? render(blocks) :
divs.map(blocks => elem('div', render(blocks))).join('\n')

Object.assign(attr, data)

return elem(attr.popover ? 'dialog' : name || 'div', attr, html)
if (this.to_block) Object.assign(attr, data)
return elem(attr.popover ? 'dialog' : name, attr, html)
},


button(data) {
const { href } = data
const label = this.renderInline(data.label || data._) || this.innerHTML || ''
Expand All @@ -65,7 +63,6 @@ const TAGS = {
return html && elem('dl', this.attr, html.join('\n'))
},


image() {
const { attr, data } = this
const { caption, href, loading = 'lazy' } = data
Expand All @@ -92,7 +89,7 @@ const TAGS = {

const content = data._
delete data._
Object.assign(attr, data)
if (this.to_inline) Object.assign(attr, data)

return elem(name, attr, this.renderInline(content, opts))
},
Expand Down Expand Up @@ -131,7 +128,6 @@ const TAGS = {
return elem('video', attr, this.innerHTML)
},


// shortcut
'!': function() {
const tag = getMimeType(this.data._).startsWith('video') ? TAGS.video : TAGS.image
Expand Down Expand Up @@ -164,9 +160,11 @@ export function renderIcon(name, symbol, icon_dir) {

export function renderTag(tag, opts = {}) {
const tags = { ...TAGS, ...opts.tags }
const tag_fn = !tag.name || tag.to_block ? 'block' : tag.to_inline ? 'inline' : tag.name
const tag_fn = tag.to_block ? 'block' : tag.to_inline ? 'inline' : tag.name
const fn = tags[tag_fn]

if (!(tag.is_block || tag.is_inline)) console.log('\n', tag)

if (!fn) {
// native html tags
if (HTML_TAGS.includes(tag.name)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/nuemark/test/block.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('block html tag without children with content', () => {
expect(html).toBe('<section>content</section>\n<p>no content</p>')
})

test.skip('block html tag with starting ul', () => {
test('block html tag with starting ul', () => {
const { blocks } = parseBlocks(['[div]', ' - hi', ' - hello'])
expect(blocks.length).toBe(1)
expect(blocks[0].blocks.length).toBe(1)

Check failure on line 62 in packages/nuemark/test/block.test.js

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, bun)

TypeError: undefined is not an object (evaluating 'blocks[0].blocks.length')

at <anonymous> (/home/runner/work/nue/nue/packages/nuemark/test/block.test.js:62:20)

Check failure on line 62 in packages/nuemark/test/block.test.js

View workflow job for this annotation

GitHub Actions / test (macos-latest, bun)

TypeError: undefined is not an object (evaluating 'blocks[0].blocks.length')

at <anonymous> (/Users/runner/work/nue/nue/packages/nuemark/test/block.test.js:62:20)
Expand Down

0 comments on commit d658399

Please sign in to comment.