Skip to content

Show every file of multi-file React examples - #2795

Open
vaibhav375 wants to merge 1 commit into
freeCodeCamp:mainfrom
vaibhav375:fix/2615-react-sandpack-files
Open

vaibhav375 wants to merge 1 commit into
freeCodeCamp:mainfrom
vaibhav375:fix/2615-react-sandpack-files

Conversation

@vaibhav375

Copy link
Copy Markdown

Fixes #2615

react.dev's interactive examples (Sandpack) only server-render the file whose tab is active. The other files are just tab buttons in the HTML, and their source is only in the page's __NEXT_DATA__ script. The core clean_html filter drops that script and clean_html_react_dev drops the tab buttons, so every multi-file example ends up as a single unnamed code block.

Changes

  • New react/sandpack_files_react_dev filter, inserted before clean_html for the react.dev versions (19 and 18). It reads the Sandpack examples from __NEXT_DATA__ and replaces each rendered example with one _pre-heading + <pre> per tab, in tab order. File paths follow react.dev's createFileMap.
  • Rendered examples are paired with the page data in order, by content: each one takes the next example in the data that has all of its tabs and the same code in the active file. package.json is compared as JSON, because Sandpack adds an empty devDependencies when it renders it.
  • Recipes and Challenges only render their first example, so the data can have examples that aren't on the page. The next example is trusted directly only while every earlier example matched the very next one and it isn't inside a Recipes/Challenges block. Otherwise every candidate has to show the same files. If that doesn't hold, or nothing matches, the example is left as it is today, so a block never shows files from a different example.
  • clean_html_react_dev keeps the _pre-heading class and no longer overwrites data-language, so CSS/HTML/JSON files get the right highlighting. JS files still use jsx.

Testing

  • test/lib/docs/scrapers/react_test.rb runs the React pipeline (19 and 18) on react.dev-shaped markup. It covers all files shown, examples with the same active file, examples that aren't rendered, hidden recipes, an example that fails to match, package.json, {...} options in the file meta, per-file language, and the fallback when the data is missing or doesn't match. Each pairing rule has a test that fails without it.
  • bundle exec rake: 846 runs, 0 failures.
  • thor docs:generate react on main vs this branch: same 182 pages and index entries; code blocks go from 2022 to 2388 with 858 file headings. Every code block from main is still there, except that the six rendered package.json files now show the source without the empty devDependencies. On <StrictMode> → "Fixing bugs found by re-running Effects in development", main shows only index.js; this branch shows index.js, App.js and StoryTray.js (checked in rackup).
  • thor docs:generate react@18: 139 pages, 751 file headings, no errors.

Examples that stay as they are today: those using SandpackWithHTMLOutput (they add a ShowRenderedHTML.js tab that isn't in the page data), a couple where the server-rendered code is cut short, and one that is ambiguous. That's 12 of the 339 examples on the 124 pages I checked. On learn/typescript the "TypeScript Playground" link inside the examples also goes away, like the CodeSandbox link already does.

react.dev only server-renders the active file of a Sandpack example; the
other files are only in the page's __NEXT_DATA__ script, which clean_html
removes. Read them from there before clean_html runs and show each file
under its name, falling back to the rendered file whenever the example
can't be paired with its data unambiguously.

Fixes freeCodeCamp#2615
@vaibhav375
vaibhav375 marked this pull request as ready for review November 21, 2026 14:44
@vaibhav375
vaibhav375 requested a review from a team as a code owner November 21, 2026 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DevDocs React documentation is missing code blocks from source documentation

1 participant