Skip to content

Commit

Permalink
merge and deduplicate class names
Browse files Browse the repository at this point in the history
  • Loading branch information
nobkd committed Feb 3, 2025
1 parent fa3a241 commit 5f05818
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/nuemark/src/parse-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ export function parseTag(input) {
const specs = strings.filter((s, i) => !i || s.match(/^[#|.]/)).join('')
const attribs = strings.filter(s => !specs.includes(s))
const self = { ...parseSpecs(specs), data: {} }

const classes = new Set((self.attr?.class || '').split(' '))

function set(key, val) {
if (key == 'class') return val.split(' ').forEach(v => classes.add(v))

const ctx = ATTR.includes(key) || key.startsWith('data-') ? 'attr' : 'data'
self[ctx][key] = val
}
Expand All @@ -30,6 +32,8 @@ export function parseTag(input) {
else set('_', getValue(key) || key)
}

if (classes.size) self.attr.class = [...classes].join(' ')

return self
}

Expand Down Expand Up @@ -76,9 +80,9 @@ export function parseAttr(str) {
const attr = {}

// classes
const classes = []
str.replace(/\.([\w\-]+)/g, (_, el) => classes.push(el))
if (classes[0]) attr.class = classes.join(' ')
const classes = new Set()
str.replace(/\.([\w\-]+)/g, (_, el) => classes.add(el))
if (classes.size) attr.class = [...classes].join(' ')

// id
str.replace(/#([\w\-]+)/, (_, el) => attr.id = el)
Expand Down
6 changes: 6 additions & 0 deletions packages/nuemark/test/block.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ test('complex tag data', () => {
expect(comp.data).toEqual({ world: true, size: 10, foo: "bar", })
})

test('duplicate tag classes', () => {
const { blocks } = parseBlocks(['[hello.c.c.bar class="foo bar" world]'])
expect(blocks[0].attr.class).toBe('c bar foo')
expect(blocks[0].data).toEqual({ world: true })
})

test('escaping', () => {
const html = renderLines([
'\\[code]', '',
Expand Down

0 comments on commit 5f05818

Please sign in to comment.