Skip to content

Commit

Permalink
Nuemark (2): ***super strong*** formatting support #379
Browse files Browse the repository at this point in the history
  • Loading branch information
tipiirai committed Oct 21, 2024
1 parent 4820038 commit b3d0d3d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 123 deletions.
13 changes: 10 additions & 3 deletions packages/nuemark/src/parse-inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { parseTag, parseAttr } from './parse-tag.js'


export const FORMATTING = {
'***': 'EM',
'___': 'EM',
'**': 'strong',
'__': 'strong',

Expand All @@ -27,11 +29,12 @@ const SIGNIFICANT = /[\*_\["`~\\/|•{\\<>]|!\[/

const PARSERS = [

// character escaping first
// \{ escaped }
(str, char0) => {
if (char0 == '\\') return { text: str.slice(1, 2), end: 2 }
},

// &lt; and &gt;
(str, char0) => {
const text = ESCAPED[char0]
if (text) return { text, end: 1 }
Expand All @@ -43,9 +46,13 @@ const PARSERS = [
if (str.startsWith(fmt)) {
const len = fmt.length
const i = str.indexOf(fmt, len + 1)
const body = str.slice(len, i)

// no spaces before/after the body
if (i == -1 || body.length != body.trim().length) return { text: str }

const tag = FORMATTING[fmt]
return i == -1 || str[len] == ' ' ? { text: str.slice(0, len) } :
{ is_format: true, tag, body: str.slice(len, i), end: i + len }
return { is_format: true, tag, body, end: i + len }
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/nuemark/src/render-inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function renderToken(token, opts={}) {

function formatText({tag, body }, opts) {
const html = tag == 'code' ? renderCode(body) : renderInline(body, opts)
return elem(tag, html)
return tag == 'EM' ? elem('em', elem('strong', html)) : elem(tag, html)
}

function renderCode(code) {
Expand Down
13 changes: 13 additions & 0 deletions packages/nuemark/test/inline.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ test('formatting', () => {
}
})

test('formatting and spaces', () => {
expect(renderInline('**hello**')).toBe('<strong>hello</strong>')
expect(renderInline('**hello*')).toBe('**hello*')
expect(renderInline('** hello **')).toBe('** hello **')
expect(renderInline('** hello**')).toBe('** hello**')
})

test('bold + em', () => {
expect(renderInline('***hello***')).toBe('<em><strong>hello</strong></em>')
expect(renderInline('___hello___')).toBe('<em><strong>hello</strong></em>')
expect(renderInline('___ hello ___')).toBe('___ hello ___')
})

test('inline render basics', () => {
const tests = [
{ text: 'hey', html: 'hey' },
Expand Down
119 changes: 0 additions & 119 deletions packages/nuemark/test/performance.js

This file was deleted.

0 comments on commit b3d0d3d

Please sign in to comment.