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(nuemark): merge and deduplicate class names #473

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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