EditableSketch / MDX leading indentation gets stripped in multiline code strings
Increasing Access
This would make writing and translating tutorials easier, especially for newer contributors.
Right now, some code examples in MDX files lose their indentation because Markdown intentionally ignores some leading spaces as part of the Markdown specification, and the Astro MDX integration follows that behavior. Contributors may also encounter invisible non-breaking space characters (U+00A0) in the files without realizing what they are or why they are there.
This can make code examples look broken or inconsistent, and can be confusing when editing tutorials. Better documentation or clearer formatting rules would help reduce this confusion and make tutorial contributions easier.
Most appropriate sub-area of p5.js?
Tutorials
Feature request details
Requested change
Document and standardize the convention of adding two extra leading spaces to multiline example code in .mdx tutorial files.
The goal is to keep indentation rendering consistent across tutorials and reduce confusion for contributors working with code example components such as EditableSketch and AnnotatedCode.
It would also help to:
- document this convention in contributor docs
- explain why invisible non-breaking space characters (
U+00A0) may appear in older files - clarify that the “extra two spaces” approach is the recommended formatting style for tutorial examples
- review other code-related MDX components affected by the same indentation issue so the convention can be applied consistently across all of them
Background
While working on tutorial translations for the p5.js website, I noticed that some .mdx files contain invisible non-breaking space characters (U+00A0) used as leading whitespace inside multiline code strings passed to EditableSketch.
Contributors mentioned that these invisible characters may have originally come from content authored in Google Docs. Since they visually resemble normal spaces, it can be difficult for contributors to notice or understand why they exist.
After discussion with contributors, we found that the root issue is that Markdown intentionally strips some leading spaces as part of the Markdown specification, and the Astro MDX integration follows that behavior. Because of this, some rendered tutorial examples lose their intended indentation.
We discussed several possible solutions, including custom preprocessing and parser-related approaches. I also experimented with a Vite plugin approach that temporarily replaced indentation with placeholder tokens before MDX parsing and restored them afterward.
However, the final consensus was that adding custom parser-related logic would make the project harder for contributors to understand and maintain long-term.
Instead, contributors preferred a simpler and more maintainable convention:
- add two extra leading spaces to each line in multiline example code
- allow Markdown/MDX to consume those spaces during parsing
- keep this formatting style consistent across tutorial documents
- Exmaple document with two-extra space style(Typography tutorial PR): https://github.com/processing/p5.js-website/pull/1170
Thank you for this detailed writeup of a helpful idea! For the next month we will be focusing on the v2 default switch (which includes making what is currently the "beta" site the main site - though redirects will maintain old links; and making a separate stable subdomain for v1).
This topic I think is not a "prerequisite" for that switch, but it seems ready to document/standardize whenever capacity allows. I did also tag "discussion" in case there's additional ideas or input, particularly on validation/testing (either e2e or preventing regression) for the desired example space behavior. Although after v2 default, p5.js v1 will be frozen, and the focus will be on v2 website, this kind of change could be applied in both sites.
Nice to have / I can try to do it if I have time at some point: the main contributor discussion on this happened on discord ([1] and [2] - it would be good to add a comment either copying important messages from nbogie and davepagurek, or summarizing them (with attribution - could also be small notes in the issue body). Since I do not know the longevity of those discord links, and they are not accessible to anyone who is not on the discord.