How to disable indented code block in markdown astro

308 Views Asked by At

I want to disable indented code blocks since I find them very unintuitive in my markdown. I tried following https://www.npmjs.com/package/remark-disable-tokenizers how it seems this is not up to date with the remark-parse library.

I note that remark-parse now uses micomark so maybe there is some configuration that could be changed there.

As a temporary solution I switched to using mdx in astro which disables indented blocks.

1

There are 1 best solutions below

0
On

Exploring how mdx.js disable indented code blocks I was able to get a working solution. Create a custom remark plugin with the following plugin (can be made more generic).

export default function disableIndentedCode() {
  const data = this.data();
  add("micromarkExtensions", {
    disable: { null: ["codeIndented"] },
  });

  /**
   * @param {string} field
   * @param {unknown} value
   */
  function add(field, value) {
    const list = /** @type {Array<unknown>} */ (
      // Other extensions
      /* c8 ignore next 2 */
      data[field] ? data[field] : (data[field] = [])
    );

    list.push(value);
  }
}