Skip to content

Commit

Permalink
feat: return early in markdown transformer
Browse files Browse the repository at this point in the history
return an empty string immediately if the input value is falsy
  • Loading branch information
cossssmin committed Aug 29, 2024
1 parent 5f6e5f7 commit daea021
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
19 changes: 14 additions & 5 deletions src/transformers/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@ import { defu as merge } from 'defu'
import md from 'posthtml-markdownit'
import posthtmlConfig from '../posthtml/defaultConfig.js'

export async function markdown(html = '', options = {}, posthtmlOptions = {}) {
export async function markdown(input = '', options = {}, posthtmlOptions = {}) {
/**
* Automatically wrap in <md> tag, unless manual mode is enabled
* With manual mode, user must wrap the markdown content in a <md> tag
* If no input is provided, return an empty string.
*/
if (!input) {
return ''
}

/**
* Automatically wrap in <md> tag, unless manual mode is enabled.
*
* With manual mode, user must wrap the input in a <md> tag.
*
* https://github.com/posthtml/posthtml-markdownit#usage
*/
html = options.manual ? html : `<md>${html}</md>`
input = options.manual ? input : `<md>${input}</md>`

return posthtml([
md(options)
])
.process(html, merge(posthtmlOptions, posthtmlConfig))
.process(input, merge(posthtmlOptions, posthtmlConfig))
.then(result => result.html)
}
18 changes: 13 additions & 5 deletions test/transformers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,19 @@ describe.concurrent('Transformers', () => {
})

test('Markdown', async () => {
const result = await markdown('# Foo\n_foo_')
const result2 = await markdown('<md tag="section"># Foo\n_foo_</md>', { manual: true })
expect(await markdown('')).toBe('')

expect(
await markdown('maizzle.com', { markdownit: { linkify: true } })
).toBe('<p><a href="http://maizzle.com">maizzle.com</a></p>\n')

expect(await markdown('# Foo\n_foo_'))
.toBe('<h1>Foo</h1>\n<p><em>foo</em></p>\n')

expect(
await markdown('<md tag="section"># Foo\n_foo_</md>', { manual: true })
).toBe('<section>\n<h1>Foo</h1>\n<p><em>foo</em></p>\n</section>')

expect(result).toBe('<h1>Foo</h1>\n<p><em>foo</em></p>\n')
expect(result2).toBe('<section>\n<h1>Foo</h1>\n<p><em>foo</em></p>\n</section>')
expect(
await useTransformers('# Foo\n_foo_', { markdown: false }).then(({ html }) => html)
).toBe('# Foo\n_foo_')
Expand Down Expand Up @@ -583,7 +591,7 @@ describe.concurrent('Transformers', () => {
expect(await replaceStrings('initial text', { 'initial': 'updated' })).toBe('updated text')
expect(await replaceStrings('initial [text]', { '(initial) \\[(text)\\]': '($2) updated' })).toBe('(text) updated')
expect(await replaceStrings('«initial» «text»', { '«(.*?)»' : '«&nbsp;$1&nbsp;»' })).toBe('«&nbsp;initial&nbsp;» «&nbsp;text&nbsp;»')

expect(
await useTransformers('initial text', { replaceStrings: { 'initial': 'updated' } }).then(({ html }) => html)
).toBe('updated text')
Expand Down

0 comments on commit daea021

Please sign in to comment.