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: add filename to meta #27

Merged
merged 3 commits into from
Aug 4, 2023
Merged
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
9 changes: 8 additions & 1 deletion src/lib/remark.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ export default function (options = {}) {
console.warn(`ExampleComponent is deprecated, use defaults.Wrapper instead`)
}

return function transformer(tree) {
return function transformer(tree, file) {
let examples = []

const filename = toPOSIX(file.filename).split(toPOSIX(file.cwd)).pop()

visit(tree, 'code', (node) => {
const languages = ['svelte', 'html']
/**
* @type {Record<string, any>}
*/
const meta = {
Wrapper: path.resolve(_dirname, 'Example.svelte'),
filename,
...defaults,
...parseMeta(node.meta || '')
}
Expand Down Expand Up @@ -154,3 +157,7 @@ function createExampleComponent(value, meta, index) {
<slot slot="code">{@html ${JSON.stringify(highlighted)}}</slot>
</Example>`
}

function toPOSIX(path) {
return path.replace(/\\/g, '/')
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
export let meta
</script>

<pre id="meta">
{JSON.stringify(meta)}
</pre>
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="../Wrapper.svelte"
```svelte example custom=["hello/world"] Wrapper="../MetaWrapper.svelte"

```
3 changes: 3 additions & 0 deletions src/routes/tests/meta/filename/+page.svx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```svelte example Wrapper="../MetaWrapper.svelte"
<div>hello</div>
```
22 changes: 18 additions & 4 deletions src/routes/tests/meta/meta.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,26 @@ test('hideStyle', async ({ page }) => {

test('wrapper and custom meta', async ({ page }) => {
await page.goto('/tests/meta/wrapper')
await expect(page.locator(`text={"Wrapper":"../Wrapper.svelte","example":true}`)).toBeVisible()
const meta = await getMeta(page)

expect(meta.Wrapper).toBe('../MetaWrapper.svelte')
expect(meta.example).toBe(true)
})

test('array meta', async ({ page }) => {
await page.goto('/tests/meta/array')
await expect(
page.locator('text={"Wrapper":"../Wrapper.svelte","example":true,"custom":["hello/world"]}')
).toBeVisible()
const meta = await getMeta(page)

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

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

expect(meta.filename).toBe('/src/routes/tests/meta/filename/+page.svx')
})

async function getMeta(page) {
return JSON.parse(await page.locator('#meta').textContent())
}
2 changes: 1 addition & 1 deletion src/routes/tests/meta/wrapper/+page.svx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
```svelte example Wrapper="../Wrapper.svelte"
```svelte example Wrapper="../MetaWrapper.svelte"

```
Loading