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

Add more options to playground, directives, format, etc #2295

Merged
merged 15 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
11 changes: 11 additions & 0 deletions docs/_asset/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,17 @@ button.success {
margin-block: calc(1 / 1.2 * (1em + 1ex));
}

.playground-editor fieldset {
border: 0;
padding: 0;
margin: 0;
min-width: 0;
}

.playground-editor fieldset label {
display: inline;
}

.frame {
/* gray-1 is used for unselected tabs, but gray-2 is really too much
* This is a perfect mix between the two: */
Expand Down
101 changes: 96 additions & 5 deletions docs/_component/editor.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import {statistics} from 'vfile-statistics'
import {reporter} from 'vfile-reporter'
import {evaluate} from '@mdx-js/mdx'
import remarkGfm from 'remark-gfm'
import rehypeRaw from 'rehype-raw'
import remarkFrontmatter from 'remark-frontmatter'
import remarkDirective from 'remark-directive'
import remarkMath from 'remark-math'
import {visit as visitEstree} from 'estree-util-visit'
import {removePosition} from 'unist-util-remove-position'
import CodeMirror from 'rodemirror'
import {basicSetup} from 'codemirror'
import {markdown as langMarkdown} from '@codemirror/lang-markdown'
Expand All @@ -25,34 +29,56 @@ lowlight.registerLanguage('js', javascript)
lowlight.registerLanguage('json', json)
lowlight.registerLanguage('md', markdown)

export function removePositionEsast(tree) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Can you remove the export? Don’t think that’s needed
  • Can you stick with the style of using calls first, definitions later (somewhere at the bottom probably!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the export, typo

Not sure what you mean by "calls first", isn't it already the case? I used the code style found in unist-util-remove-position

function removePositionEsast(tree) {
  visitEstree(tree, remove)
  return tree

  function remove(node) {
    delete node.loc
    delete node.start
    delete node.end
    delete node.range
  }
}

Or is it the top-level fn declaration that I should put at the bottom of the file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I mean, place it in the highest scope, at the bottom!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

But It was already at the highest scope, and useMdx() is at the top. So we have one helper fn above and under the default exported component 🤪

visitEstree(tree, remove)
return tree

function remove(node) {
delete node.loc
delete node.start
delete node.end
delete node.range
}
}

function useMdx(defaults) {
const [state, setState] = useState({...defaults, file: null})
const {run: setConfig} = useDebounceFn(
async (config) => {
const file = new VFile({basename: 'example.mdx', value: config.value})
const basename = config.formatMd ? 'example.md' : 'example.mdx'
const file = new VFile({basename, value: config.value})

const capture = (name) => () => (tree) => {
file.data[name] = tree
}

const remarkPlugins = []

if (config.gfm) remarkPlugins.push(remarkGfm)
if (config.frontmatter) remarkPlugins.push(remarkFrontmatter)
if (config.math) remarkPlugins.push(remarkMath)

if (config.directive) remarkPlugins.push(remarkDirective)
remarkPlugins.push(capture('mdast'))

const rehypePlugins = []
if (config.rehypeRaw) rehypePlugins.push(rehypeRaw)
slorber marked this conversation as resolved.
Show resolved Hide resolved
rehypePlugins.push(capture('hast'))

try {
file.result = (
await evaluate(file, {
...runtime,
useDynamicImport: true,
remarkPlugins,
rehypePlugins: [capture('hast')],
rehypePlugins,
recmaPlugins: [capture('esast')]
})
).default

if (!config.position) {
removePosition(file.data.mdast, {force: true})
removePosition(file.data.hast, {force: true})
removePositionEsast(file.data.esast)
}
} catch (error) {
const message =
error instanceof VFileMessage ? error : new VFileMessage(error)
Expand Down Expand Up @@ -104,9 +130,13 @@ export function Editor({children}) {
const defaultValue = children
const extensions = useMemo(() => [basicSetup, oneDark, langMarkdown()], [])
const [state, setConfig] = useMdx({
formatMd: false,
position: false,
gfm: false,
frontmatter: false,
directive: false,
math: false,
rehypeRaw: false,
value: defaultValue
})
const onUpdate = useCallback(
Expand All @@ -129,7 +159,7 @@ export function Editor({children}) {
}, [state])

return (
<div>
<div className="playground-editor">
<Tabs className="frame">
<TabList className="frame-tab-bar frame-tab-bar-scroll">
<Tab
Expand Down Expand Up @@ -163,6 +193,41 @@ export function Editor({children}) {
</TabPanel>
<TabPanel>
<form className="frame-body frame-body-box frame-body-box-fixed-height">
<fieldset>
<label>
<input
type="radio"
name="language"
checked={!state.formatMd}
onChange={() => {
setConfig({...state, formatMd: false})
}}
/>{' '}
Use <code>MDX</code>
</label>
<span style={{margin: '1em'}}>{' | '}</span>
slorber marked this conversation as resolved.
Show resolved Hide resolved
<label>
<input
type="radio"
name="language"
checked={state.formatMd}
onChange={() => {
setConfig({...state, formatMd: true})
}}
/>{' '}
Use <code>CommonMark</code>
</label>
</fieldset>
<label>
<input
checked={state.position}
type="checkbox"
onChange={() =>
setConfig({...state, position: !state.position})
}
/>{' '}
Show positional info
</label>
<label>
<input
checked={state.gfm}
Expand Down Expand Up @@ -198,6 +263,32 @@ export function Editor({children}) {
<code>remark-math</code>
</a>
</label>
<label>
<input
checked={state.directive}
type="checkbox"
onChange={() =>
setConfig({...state, directive: !state.directive})
}
/>{' '}
Use{' '}
wooorm marked this conversation as resolved.
Show resolved Hide resolved
<a href="https://github.com/remarkjs/remark-directive">
<code>remark-directive</code>
</a>
</label>
<label>
<input
checked={state.rehypeRaw}
type="checkbox"
onChange={() =>
setConfig({...state, rehypeRaw: !state.rehypeRaw})
}
/>{' '}
Use{' '}
<a href="https://github.com/rehypejs/rehype-raw">
<code>rehype-raw</code>
</a>
</label>
</form>
</TabPanel>
</Tabs>
Expand Down
57 changes: 57 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"rehype-slug": "^5.0.0",
"rehype-stringify": "^9.0.0",
"remark-cli": "^11.0.0",
"remark-directive": "^2.0.1",
slorber marked this conversation as resolved.
Show resolved Hide resolved
"remark-frontmatter": "^4.0.0",
"remark-gemoji": "^7.0.0",
"remark-gfm": "^3.0.0",
Expand All @@ -104,6 +105,8 @@
"unified": "^10.0.0",
"unist-builder": "^3.0.0",
"unist-util-visit": "^4.0.0",
"estree-util-visit": "^1.2.1",
"unist-util-remove-position": "^4.0.2",
"uvu": "^0.5.0",
"vfile": "^5.0.0",
"vfile-message": "^3.0.0",
Expand Down