-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 2 commits
d92eed1
a4a194d
ef61762
ca34275
8142872
d478105
748281f
07a1ddb
62ea85d
bbc84f5
68d707d
3e1b89e
90409dc
8a1b0ff
cc12b73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,9 @@ import {reporter} from 'vfile-reporter' | |
import {evaluate} from '@mdx-js/mdx' | ||
import remarkGfm from 'remark-gfm' | ||
import remarkFrontmatter from 'remark-frontmatter' | ||
import remarkDirective from 'remark-directive' | ||
import remarkMath from 'remark-math' | ||
import {removePosition} from 'unist-util-remove-position' | ||
import CodeMirror from 'rodemirror' | ||
import {basicSetup} from 'codemirror' | ||
import {markdown as langMarkdown} from '@codemirror/lang-markdown' | ||
|
@@ -29,7 +31,8 @@ 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 | ||
|
@@ -40,6 +43,7 @@ function useMdx(defaults) { | |
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')) | ||
|
||
|
@@ -53,6 +57,12 @@ function useMdx(defaults) { | |
recmaPlugins: [capture('esast')] | ||
}) | ||
).default | ||
|
||
if (!config.position) { | ||
removePosition(file.data.mdast, {force: true}) | ||
removePosition(file.data.hast, {force: true}) | ||
removePosition(file.data.esast, {force: true}) | ||
} | ||
} catch (error) { | ||
const message = | ||
error instanceof VFileMessage ? error : new VFileMessage(error) | ||
|
@@ -104,8 +114,11 @@ 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, | ||
value: defaultValue | ||
}) | ||
|
@@ -198,6 +211,46 @@ 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> | ||
<hr /> | ||
slorber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<label> | ||
<input | ||
checked={state.formatMd} | ||
type="checkbox" | ||
onChange={() => | ||
setConfig({...state, formatMd: !state.formatMd}) | ||
} | ||
/>{' '} | ||
Use{' '} | ||
<a href="https://mdxjs.com/packages/mdx/#optionsformat"> | ||
<code>format: 'md'</code> | ||
</a> | ||
</label> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should add a tooltip here to explain MDX infers this based on the input file extension by default. I think this option makes sense for the playground, but it’s best to discourage using this option. Actually, maybe a radio button is more appropriate here than a checkbox, although the checkbox currently fits nice in the UI. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes a radio or select can also work, open to change a bit the UI, not sure how to organize now that it's not just a list of remark plugins.
Why? Docusaurus uses MDX and I think supporting a commonmark mode can help existing docs sites to adopt it more easily, ie to have something less strict running asap without a ton of compilation errors to fix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Let’s wait for @wooorm’s feedback, as I think he designed the website. I think it looks great already. :)
This option changes whether files are treated as commonmark or MDX. Leaving it off, means this is inferred from the file extension. It’s not about strictness (although basically anything is technically valid markdown, but MDX has syntax errors). This option is useful when the file extension is unknown (such as in this playground), but I wouldn’t recommend it otherwise. I don’t know how Docusaurus uses it exactly though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On my side, I find it helpful to understand the MDX compiler behavior when handling .md files. I'm not really saying the Although it's not recommended, Docusaurus historically has many sites that use MDX with a For this reason, it's likely I'll add an option There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can pass the filename. await compile({
value: '# Hello <strong>world</strong>!',
path: '/home/me/hello.mdx'
}) I would also be cautious treating I.e. given the following content: <div>
foo
bar
</div> In markdown, this is equivalent to the following HTML: <div> foo bar </div> In MDX, this is equivalen to the following JSX: <div>
<p>foo</p>
<p>bar</p>
</div> I understand it’s useful to add the option for migration purposes in Docusaurus.
I think this may have been misinterpreted. I means it’s best to discourage using this option in general, but there are definitely legitimate use cases. The playground, where there are no actual file names, is a good use case for such an option. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a radio toggle like this is better: https://wooorm.com/markdown-tm-language/, https://github.com/wooorm/markdown-tm-language/blob/656e7dea3f5248c728a6eeda4e9780778bd6449f/example/index.jsx#L412-L437. Don’t “connect” it to an option. But allow people to choose whether the input is plain markdown or MDX There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
<label> | ||
<input | ||
checked={state.position} | ||
type="checkbox" | ||
onChange={() => | ||
setConfig({...state, position: !state.position}) | ||
} | ||
/>{' '} | ||
Use{' '} | ||
<a href="https://github.com/syntax-tree/unist#position"> | ||
<code>position</code> | ||
</a> | ||
slorber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</label> | ||
</form> | ||
</TabPanel> | ||
</Tabs> | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW not sure this works for esast, is there a tool to remove esast positions to make exploring the AST more convenient?
I mean these nodes:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can try
unist-util-position-from-estree
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@remcohaszing that only gets a position, it doesn’t clear start/end/loc.
@slorber I don’t think there is one, in some tests I’ve used a walker/visit and called
delete
for those fieldsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced with: