Skip to content

Commit a62cf09

Browse files
committed
WIP
1 parent b1328c7 commit a62cf09

File tree

10 files changed

+200
-14
lines changed

10 files changed

+200
-14
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
]
6262
},
6363
"dependencies": {
64+
"@mdx-js/mdx": "^2.1.3",
6465
"@next/mdx": "^12.3.0",
6566
"github-slugger": "^1.4.0",
6667
"gray-matter": "^4.0.3",
@@ -77,10 +78,12 @@
7778
"rehype-autolink-headings": "^6.1.1",
7879
"rehype-sanitize": "^5.0.1",
7980
"rehype-slug": "^5.0.1",
81+
"remark-comment": "^1.0.0",
8082
"remark-external-links": "^9.0.1",
8183
"remark-gfm": "^3.0.1",
8284
"remark-heading-id": "^1.0.0",
8385
"remark-inline-links": "^6.0.1",
86+
"remark-parse": "^10.0.1",
8487
"strip-indent": "^4.0.0",
8588
"unist-util-visit": "^4.1.1",
8689
"vfile": "^5.3.5"

pnpm-lock.yaml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable react/display-name */
22
import React from 'react'
33
import { MDXRemote } from 'next-mdx-remote'
4-
// import { State, Observe } from 'mdx-observable'
4+
import { State, Observe } from 'mdx-observable'
55
// import Tabs, { Tab } from './components/Tabs.jsx'
66
// import Interpolate from './components/Interpolate.jsx'
77
import CodeBlock from './components/CodeBlock.jsx'
@@ -20,7 +20,9 @@ export function Documentation({ source, theme, additionalComponents = {} }) {
2020
)
2121
},
2222
pre: (props) => <CodeBlock {...props} theme={theme} />,
23-
code: (props) => <InlineCode {...props} theme={theme} />
23+
code: (props) => <InlineCode {...props} theme={theme} />,
24+
State,
25+
Observe,
2426
},
2527
additionalComponents
2628
)

src/lib/remark-plugins/state/index.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1+
const COMMENT_LABEL = 'export-to-input'
2+
3+
const isClosingComment = (child) => {
4+
return (
5+
child.type === 'comment' && child.commentValue.trim().includes(`/${COMMENT_LABEL}`)
6+
)
7+
}
8+
9+
const isOpeningComment = (child) => {
10+
11+
return (
12+
child.type === 'comment' && child.commentValue.trim().includes(COMMENT_LABEL)
13+
)
14+
}
15+
116
function findVarsToSub(children) {
217
const varsToSub = []
318

419
const updatedChildren = children.reduce((arr, child) => {
5-
const isClosingInputComment =
6-
child.type === 'comment' &&
7-
child.value.trim().includes('/export-to-input')
20+
const isClosingInputComment = isClosingComment(child)
821

922
if (isClosingInputComment) {
1023
// find opening comment
1124
const numberOfElementsToRemove = [...arr].reverse().findIndex((c) => {
12-
return (
13-
c.type === 'comment' && c.value.trim().includes('export-to-input')
14-
)
25+
return isOpeningComment(c)
1526
})
1627

1728
const arrWithoutComment = arr.slice(0, -numberOfElementsToRemove)

src/serialize.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import remarkInlineLinks from 'remark-inline-links'
1111
import remarkHeadingId from './lib/remark-plugins/heading-ids'
1212
// TODO: plugins require some refactoring, see https://github.com/storybookjs/storybook/issues/9602 for inspiration/guidance
1313
// import remarkTabs from './lib/remark-plugins/tabs'
14-
// import remarkState from './lib/remark-plugins/state'
14+
import remarkState from './lib/remark-plugins/state'
1515
// import remarkSections from './lib/remark-plugins/sections'
1616
import remarkGfm from 'remark-gfm'
1717
import remarkExternalLinks from 'remark-external-links'
@@ -111,7 +111,7 @@ export async function pageProps({ params }) {
111111
remarkHeadingId,
112112
// remarkSections,
113113
// remarkTabs,
114-
// remarkState,
114+
remarkState,
115115
remarkGfm,
116116
[
117117
remarkCodeImport,
File renamed without changes.

test/__snapshots__/Documentation.test.jsx.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22

33
exports[`Document > should render 1`] = `
44
<div>
5+
&lt;State initialstate={
6+
{
7+
8+
}
9+
}&gt;
10+
11+
12+
{({setState: setSubstitutionState, ...substitutionState}) =&gt; (
13+
14+
15+
&lt;&gt;
16+
17+
18+
&lt;Interpolate substitutions={substitutionState}&gt;
19+
20+
521
<hr />
622
723
@@ -117,5 +133,17 @@ exports[`Document > should render 1`] = `
117133
<p>
118134
Second Paragraph.
119135
</p>
136+
137+
138+
&lt;/Interpolate&gt;
139+
140+
141+
&lt;/&gt;
142+
143+
144+
)}
145+
146+
147+
&lt;/State&gt;
120148
</div>
121149
`;
File renamed without changes.

test/remark-plugins/state.test.jsx

Lines changed: 132 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ import { State, Observe } from 'mdx-observable'
55
import Interpolate from '../../src/components/Interpolate'
66
import userEvent from '@testing-library/user-event'
77
import MDX from '@mdx-js/runtime'
8-
import { describe, it, expect } from 'vitest'
8+
import { describe, it, expect, afterAll } from 'vitest'
9+
import { serialize } from 'next-mdx-remote/serialize'
10+
import { MDXRemote } from 'next-mdx-remote'
11+
import remarkParse from 'remark-parse'
12+
import remarkComment from 'remark-comment'
13+
14+
const md = `
15+
# Hi folks!
16+
17+
Here is some text for you. How are you doing today?
918
10-
const markdown = `
1119
<!--export-to-input-->
1220
1321
~~~bash
@@ -21,8 +29,96 @@ export OPSTRACE_CLUSTER_NAME=<choose_a_name>
2129
~~~
2230
`
2331

32+
const mdx = `
33+
# Hi folks!
34+
35+
Here is some text for you. How are you doing today?
36+
37+
{/* export-to-input */}
38+
39+
~~~bash
40+
export OPSTRACE_CLUSTER_NAME=<choose_a_name>
41+
~~~
42+
43+
{/* /export-to-input */}
44+
45+
~~~bash
46+
./opstrace destroy aws $OPSTRACE_CLUSTER_NAME
47+
~~~
48+
`
49+
50+
const { ...originalEnv } = process.env
51+
52+
afterAll(async () => {
53+
process.env = originalEnv
54+
})
55+
2456
describe('remarkState', () => {
25-
it('test parsing', async () => {
57+
it.only('test parsing MD', async () => {
58+
const mdxSource = await serialize(md, { format: 'mdx',
59+
mdxOptions: {
60+
remarkPlugins: [remarkState, remarkParse, [remarkComment, { ast: true }]]
61+
}
62+
})
63+
const testComponent = render(
64+
<MDXRemote
65+
{...mdxSource}
66+
components={{
67+
Interpolate,
68+
State,
69+
Observe
70+
}}
71+
/>
72+
)
73+
74+
// // the commented export statement is not rendered
75+
// expect(
76+
// testComponent.queryByText('export OPSTRACE_CLUSTER_NAME=<choose_a_name>')
77+
// ).not.toBeInTheDocument()
78+
79+
// // default value is shown
80+
// expect(
81+
// testComponent.getByText('./opstrace destroy aws $OPSTRACE_CLUSTER_NAME')
82+
// ).toBeInTheDocument()
83+
84+
// // typing my custon name
85+
// const value = 'MyService'
86+
// const input = testComponent.getByRole('textbox')
87+
// await userEvent.type(input, value)
88+
89+
// // custom name is shown
90+
// expect(
91+
// await testComponent.findByText(`./opstrace destroy aws ${value}`)
92+
// ).toBeInTheDocument()
93+
94+
expect(testComponent.container).toMatchInlineSnapshot(`
95+
<div>
96+
<h1>
97+
Hi folks!
98+
</h1>
99+
<p>
100+
Here is some text for you. How are you doing today?
101+
</p>
102+
<input
103+
class="shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md mb-5"
104+
placeholder="$OPSTRACE_CLUSTER_NAME"
105+
type="text"
106+
value="MyService"
107+
/>
108+
<pre>
109+
<code
110+
class="language-bash"
111+
>
112+
./opstrace destroy aws MyService
113+
114+
</code>
115+
</pre>
116+
</div>
117+
`)
118+
})
119+
120+
it.skip('test parsing MDX', async () => {
121+
process.env.DOCS_USE_MDX = 'true'
26122
const testComponent = render(
27123
<MDX
28124
components={{
@@ -32,7 +128,7 @@ describe('remarkState', () => {
32128
}}
33129
remarkPlugins={[remarkState]}
34130
>
35-
{markdown}
131+
{mdx}
36132
</MDX>
37133
)
38134

@@ -55,5 +151,37 @@ describe('remarkState', () => {
55151
expect(
56152
await testComponent.findByText(`./opstrace destroy aws ${value}`)
57153
).toBeInTheDocument()
154+
155+
expect(testComponent.container).toMatchInlineSnapshot(`
156+
<div>
157+
<h1>
158+
Hi folks!
159+
</h1>
160+
<p>
161+
Here is some text for you. How are you doing today?
162+
</p>
163+
<p>
164+
{/
165+
<em>
166+
export-to-input
167+
</em>
168+
/}
169+
</p>
170+
<input
171+
class="shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md mb-5"
172+
placeholder="$OPSTRACE_CLUSTER_NAME"
173+
type="text"
174+
value="MyService"
175+
/>
176+
<pre>
177+
<code
178+
class="language-bash"
179+
>
180+
./opstrace destroy aws MyService
181+
182+
</code>
183+
</pre>
184+
</div>
185+
`)
58186
})
59187
})

0 commit comments

Comments
 (0)