Skip to content

Commit

Permalink
feat: add option wrapHTML
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Apr 26, 2019
1 parent 927809e commit f56375a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@ Options for [markdown-it](https://markdown-it.github.io/markdown-it/).

A function whose first argument is the MarkdownIt instance.

##### options.wrapHTML

- Type: `(html: string) => string`
- Default: <code>html => &#x60;<div class="vmark">${html}</div>&#x60;</code>

Wrap the HTML in an element, because the `<template>` block in Vue SFC doesn't allow multiple root elements. You can use this option to wrap HTML inside a Vue component:

```js
const wrapHTML = html => `<render-html>${html}</render-html>`
```

Then inside the `<render-html>` component, you can access the `html` like this:

```vue
<template>
<div class="content">
<!-- `html` is available as default slot -->
<slot name="default"></slot>
</div>
</template>
```

## Contributing

1. Fork it!
Expand Down
7 changes: 5 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const getMarkdown = require('./markdown')

const defaultWrapHTML = html => `<div class="vmark">${html}</div>`

module.exports = (input, options = {}) => {
const env = {}
const template = getMarkdown(options.markdown, options.extend).render(input, env)
const html = getMarkdown(options.markdown, options.extend).render(input, env)
const wrapHTML = options.wrapHTML || defaultWrapHTML

const component = `<template>
<div class="vmark">${template}</div>
${wrapHTML(html)}
</template>
${env.hoistedTags ? env.hoistedTags.join('\n\n') : ''}`
Expand Down
9 changes: 9 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ exports[`with tags 1`] = `
}
</style>"
`;
exports[`wrap html 1`] = `
"<template>
<foo><h1>hello</h1>
</foo>
</template>
"
`;
10 changes: 8 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const vmark = require('../')

function snapshot(title, text) {
function snapshot(title, text, options) {
test(title, () => {
expect(vmark(text.trim())).toMatchSnapshot()
expect(vmark(text.trim(), options)).toMatchSnapshot()
})
}

Expand All @@ -21,3 +21,9 @@ snapshot('with tags', `
}
</style>
`)

snapshot('wrap html', `
# hello
`, {
wrapHTML: html => `<foo>${html}</foo>`
})

0 comments on commit f56375a

Please sign in to comment.