MDXEditor parse error when loading Markdown with unsupported code fence languages
I'm creating an ai_context submodule for document loader integration:
ai_context#3586275+s
and ran into an issue.
**Composer 2.5 analysis:**
You're right to question that — I shouldn't have patched `document_loader` locally. I've **reverted those changes**.
## Is a fix required?
**Yes**, but it belongs in **Document Loader**, not in `ai_context`.
The error is real. When `document_loader_mdx` loads a `.md` file into MDXEditor, content hits `sanitizeForMdx()` in `DocumentLoaderMdxForm.php`, then is inserted via `setMarkdown()`. MDXEditor only accepts fenced code blocks whose language is in its configured list (from the AI module's `Editor.jsx`: `javascript`, `python`, `bash`, etc.) and that have **no fence meta**.
Markdown with common tags like ` ```php `, ` ```yaml `, or ` ```javascript lineNumbers ` fails with:
> Parsing of the following markdown structure failed: {"type":"code","name":"N/A"}
That is a gap in **`document_loader_mdx`'s sanitizer**, which its own `ARCHITECTURE.md` describes as responsible for MDX compatibility. This is not something `ai_context_document_loader` should own long term.
## Why not fix it in `ai_context`?
`hook_document_loader_post_load_alter()` runs **before** `document_loader_mdx` sanitizes. You could normalize code fences there, but `sanitizeForMdx()` would still run afterward and:
- strip HTML inside code blocks (its regex doesn't respect fences)
- leave no hook for downstream modules to alter the final MDX-safe content
So a proper fix needs to happen in **`document_loader_mdx`** (or a new alter hook there).
## What to ask the Document Loader maintainer
File an issue on [document_loader](https://www.drupal.org/project/issues/document_loader) along these lines:
**Title:** MDXEditor parse error when loading Markdown with unsupported code fence languages
**Summary:** Loading `.md` files via the Load Document button inserts content, but MDXEditor shows a parse error for code blocks. MDXEditor's `codeMirrorPlugin` only imports fences whose language is in its allowlist and that have no meta.
**Steps:**
1. Enable `document_loader_mdx` and use an MDXEditor field (e.g. Context Item content).
2. Load a Markdown file with ` ```php ` or ` ```yaml ` fences.
3. Editor shows: `Parsing of the following markdown structure failed: {"type":"code","name":"N/A"}`
**Expected:** Content loads and renders in rich-text mode.
**Proposed fix:** Extend `sanitizeForMdx()` to:
1. Normalize unsupported fence languages to plain ` ``` ` (or map aliases like `shell` → `sh`)
2. Strip fence meta (e.g. ` ```javascript lineNumbers `)
3. Apply HTML/image/comment stripping only **outside** code fences
**Reference:** AI module languages in `ui/mdxeditor/src/components/Editor.jsx`; MDXEditor matching logic in `@mdxeditor/editor` `MdastCodeVisitor`.
## AI usage
- [x] AI assisted issue
- [x] AI assisted code
issue