Skip to content

Commit

Permalink
Merge pull request #28 from mattjennings/feat/wrapper-named-export
Browse files Browse the repository at this point in the history
feat: support named exports for wrapper components
  • Loading branch information
mattjennings committed Aug 4, 2023
2 parents 79c6883 + 2b6757c commit f6bb7f6
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 6 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ Example code blocks are rendered with a [Svelte component](./src/lib/Example.sve
examples,
{
defaults: {
Wrapper: '/src/lib/Example.svelte'
Wrapper: '/src/lib/Example.svelte',

// or if the component is a named export
Wrapper: ['some-package', 'CustomExample'] // -> import { CustomExample } from 'some-package'
}
}
]
Expand Down
1 change: 1 addition & 0 deletions playwright.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = {
testDir: './src/routes/tests',
timeout: 10000,
webServer: {
command: 'npm run build:site && npm run preview',
port: 4173
Expand Down
18 changes: 15 additions & 3 deletions src/lib/remark.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const RE_SCRIPT_START =
const RE_SCRIPT_BLOCK = /(<script[\s\S]*?>)([\s\S]*?)(<\/script>)/g
const RE_STYLE_BLOCK = /(<style[\s\S]*?>)([\s\S]*?)(<\/style>)/g

// parses key=value pairs from a string. supports strings, numbers, booleans, and arrays
const RE_PARSE_META = /(\w+=\d+|\w+="[^"]*"|\w+=\[[^\]]*\]|\w+)/g

export const EXAMPLE_MODULE_PREFIX = '___mdsvexample___'
export const EXAMPLE_COMPONENT_PREFIX = 'Mdsvexample___'

Expand Down Expand Up @@ -66,7 +69,10 @@ export default function (options = {}) {
// add imports for each generated example
let scripts = ''
examples.forEach((example, i) => {
const imp = `import Example from "${example.Wrapper}";\n`
const imp =
typeof example.Wrapper === 'string'
? `import Example from "${example.Wrapper}";\n`
: `import { ${example.Wrapper[1]} as Example } from "${example.Wrapper[0]}";\n`

if (!scripts.includes(imp)) {
scripts += imp
Expand Down Expand Up @@ -101,12 +107,18 @@ export default function (options = {}) {

function parseMeta(meta) {
const result = {}
const meta_parts = meta.match(/(?:[^\s"]+|"[^"]*")+/g) ?? []
const meta_parts = meta.match(RE_PARSE_META) ?? []

for (let i = 0; i < meta_parts.length; i++) {
const [key, value = 'true'] = meta_parts[i].split('=')

result[key] = JSON.parse(value)
try {
result[key] = JSON.parse(value)
} catch (e) {
const error = new Error(`Unable to parse meta \`${key}=${value}\` - ${e.message}`)
error.stack = e.stack
throw error
}
}

return result
Expand Down
2 changes: 1 addition & 1 deletion src/routes/tests/meta/array/+page.svx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
```svelte example custom=["hello/world"] Wrapper="../MetaWrapper.svelte"
```svelte example space=["hello", "world"] nospace=["hello","world"] Wrapper="../MetaWrapper.svelte"

```
4 changes: 3 additions & 1 deletion src/routes/tests/meta/meta.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ test('wrapper and custom meta', async ({ page }) => {

test('array meta', async ({ page }) => {
await page.goto('/tests/meta/array')

const meta = await getMeta(page)

expect(meta.custom).toEqual(['hello/world'])
expect(meta.space).toEqual(['hello', 'world'])
expect(meta.nospace).toEqual(['hello', 'world'])
})

test('filename meta', async ({ page }) => {
Expand Down
3 changes: 3 additions & 0 deletions src/routes/tests/meta/wrapper-named-export/+page.svx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```svelte example Wrapper=["../wrappers.js", "MetaWrapper"]

```
3 changes: 3 additions & 0 deletions src/routes/tests/meta/wrappers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import MetaWrapper from './MetaWrapper.svelte'

export { MetaWrapper }

0 comments on commit f6bb7f6

Please sign in to comment.