Skip to content

Commit

Permalink
fix(to-markdown): parse component props object (#84)
Browse files Browse the repository at this point in the history
Co-authored-by: Farnabaz <[email protected]>
  • Loading branch information
ManUtopiK and farnabaz authored Nov 18, 2024
1 parent 74cee31 commit 5d44379
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/to-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ export default (opts: RemarkMDCOptions = {}) => {
const attrs = Object.entries(fmAttributes)
.sort(([key1], [key2]) => key1.localeCompare(key2))
.reduce((acc, [key, value2]) => {
// Stringify only JSON objects. `{":key:": "value"}` can be used for binding data to frontmatter.
if (key?.startsWith(':') && typeof value2 !== 'string') {
// Parse only JSON objects. `{":key:": "value"}` can be used for binding data to frontmatter.
if (key?.startsWith(':') && isValidJSON(value2)) {
try {
value2 = JSON.parse(value2)
} catch {
Expand Down
73 changes: 66 additions & 7 deletions test/__snapshots__/codeblock-props.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`basic > YamlProps 1`] = `
exports[`codeblock-props > YamlProps 1`] = `
{
"children": [
{
Expand Down Expand Up @@ -61,7 +61,7 @@ key: value",
}
`;

exports[`basic > nested-component-yamlProps 1`] = `
exports[`codeblock-props > nested-component-yamlProps 1`] = `
{
"children": [
{
Expand Down Expand Up @@ -245,7 +245,7 @@ key: value",
}
`;

exports[`basic > notYamlProps 1`] = `
exports[`codeblock-props > notYamlProps 1`] = `
{
"children": [
{
Expand Down Expand Up @@ -310,7 +310,7 @@ array:
}
`;

exports[`basic > notYamlProps2 1`] = `
exports[`codeblock-props > notYamlProps2 1`] = `
{
"children": [
{
Expand Down Expand Up @@ -375,7 +375,7 @@ array:
}
`;

exports[`basic > notYamlProps3 1`] = `
exports[`codeblock-props > notYamlProps3 1`] = `
{
"children": [
{
Expand Down Expand Up @@ -440,7 +440,7 @@ array:
}
`;

exports[`basic > shouldConvertYamlProps 1`] = `
exports[`codeblock-props > shouldConvertYamlProps 1`] = `
{
"children": [
{
Expand Down Expand Up @@ -500,7 +500,66 @@ key: value
}
`;

exports[`basic > yamlProps1 1`] = `
exports[`codeblock-props > shouldConvertYamlPropsWithoutOption 1`] = `
{
"children": [
{
"attributes": {},
"children": [],
"data": {
"hName": "with-frontmatter-yaml",
"hProperties": {
":array": "["item",{"itemKey":"value"}]",
"key": "value",
},
},
"fmAttributes": {
"array": [
"item",
{
"itemKey": "value",
},
],
"key": "value",
},
"name": "with-frontmatter-yaml",
"position": {
"end": {
"column": 3,
"line": 8,
"offset": 92,
},
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"rawData": "array:
- item
- itemKey: value
key: value
---",
"type": "containerComponent",
},
],
"position": {
"end": {
"column": 3,
"line": 8,
"offset": 92,
},
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"type": "root",
}
`;

exports[`codeblock-props > yamlProps1 1`] = `
{
"children": [
{
Expand Down
6 changes: 5 additions & 1 deletion test/codeblock-props.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, describe } from 'vitest'
import { runMarkdownTests } from './utils'

describe('basic', () => {
describe('codeblock-props', () => {
runMarkdownTests({
YamlProps: {
mdcOptions: {
Expand Down Expand Up @@ -52,6 +52,10 @@ describe('basic', () => {
markdown: '::with-frontmatter-yaml\n---\narray:\n - item\n - itemKey: value\nkey: value\n---\n::',
expected: '::with-frontmatter-yaml\n```yaml [props]\narray:\n - item\n - itemKey: value\nkey: value\n```\n::'
},
// shouldConvertYamlPropsWithoutOption: {
// markdown: '::with-frontmatter-yaml\n```yaml [props]\narray:\n - item\n - itemKey: value\nkey: value\n```\n::',
// expected: '::with-frontmatter-yaml\n---\narray:\n - item\n - itemKey: value\nkey: value\n---\n::'
// },
yamlProps1: {
mdcOptions: {
experimental: {
Expand Down

0 comments on commit 5d44379

Please sign in to comment.