From 7461e82c81438df956861197536f9ceeaf63d6b3 Mon Sep 17 00:00:00 2001 From: Alex Sherwin Date: Mon, 24 Jul 2023 19:34:06 -0400 Subject: [PATCH] Add "allowHTML" option for Markdoc with HTML parsing/processing (#7597) * 7576 - initial support for HTML inside Markdoc. This uses htmlparser2 to perform a pure token transform/mutation on the markdown-it tokens, replacing the original raw HTML string tokens with a richer set of tokens per HTML node, and in the process Markdoc tags are interleaved in the resulting token graph at the appropriate locations This removes the legacy config of the @astrojs/markdoc integration entirely (suggested by @bholmesdev) and introduces a new type for options to be specified in the astro config, initially, with just the new "enableHTML" option When "enableHTML" is *not* enabled (the default), the behavior of the entire @astrojs/markdoc integration should remain functionally equivalent to before this change * 7576 - fixed issues with whitespace preservation also: * cleaned up " to ' for astro project preferred linting * made the html rendering test fixture use a dynamic path * 7576 - detailed nested HTML test coverage * 7576 - component + HTML interleaved tests * 7576 - fix lint problems from previous changes * 7576 - some commentary * 7576 - file naming, refactor html under imports, package.json exports definition for html * 7576 * move out of extensions dir, remove export * cdata handling changes * 7576 * inline license from third party code * cleanup test class copy of HTML output * remove // third party indicators for imports (clarification: not third party code, just a indicator this group of imports is third party) * 7576 - fixed test before/after for DRY'ness * 7576 - no need to React-ify HTML attribute case * 7576 - rename "enableHTML" option to "allowHTML" * Added Markdoc allowHTML feature changeset * 7576 - updated README with allowHTML info * 7576 - fixed changeset typo * 7576 - minor edits based on PR feedback for docs * 7576 - minor edits based on PR feedback for docs --- .changeset/stupid-poets-grab.md | 16 + packages/integrations/markdoc/README.md | 32 +- packages/integrations/markdoc/package.json | 2 + .../markdoc/src/content-entry-type.ts | 27 +- .../src/html/css/parse-inline-css-to-react.ts | 23 ++ .../src/html/css/parse-inline-styles.ts | 278 +++++++++++++++++ .../markdoc/src/html/css/style-to-object.ts | 70 +++++ .../integrations/markdoc/src/html/index.ts | 2 + .../markdoc/src/html/tagdefs/html.tag.ts | 32 ++ .../html/transform/html-token-transform.ts | 256 +++++++++++++++ packages/integrations/markdoc/src/index.ts | 15 +- packages/integrations/markdoc/src/options.ts | 3 + packages/integrations/markdoc/src/runtime.ts | 36 ++- .../integrations/markdoc/src/tokenizer.ts | 38 +++ .../fixtures/render-html/astro.config.mjs | 7 + .../fixtures/render-html/markdoc.config.ts | 19 ++ .../test/fixtures/render-html/package.json | 9 + .../render-html/src/components/Aside.astro | 116 +++++++ .../render-html/src/components/Mark.astro | 11 + .../src/content/blog/components.mdoc | 47 +++ .../src/content/blog/nested-html.mdoc | 18 ++ .../blog/randomly-cased-html-attributes.mdoc | 20 ++ .../render-html/src/content/blog/simple.mdoc | 11 + .../render-html/src/pages/[slug].astro | 29 ++ .../markdoc/test/render-html.test.js | 293 ++++++++++++++++++ pnpm-lock.yaml | 38 ++- 26 files changed, 1405 insertions(+), 43 deletions(-) create mode 100644 .changeset/stupid-poets-grab.md create mode 100644 packages/integrations/markdoc/src/html/css/parse-inline-css-to-react.ts create mode 100644 packages/integrations/markdoc/src/html/css/parse-inline-styles.ts create mode 100644 packages/integrations/markdoc/src/html/css/style-to-object.ts create mode 100644 packages/integrations/markdoc/src/html/index.ts create mode 100644 packages/integrations/markdoc/src/html/tagdefs/html.tag.ts create mode 100644 packages/integrations/markdoc/src/html/transform/html-token-transform.ts create mode 100644 packages/integrations/markdoc/src/options.ts create mode 100644 packages/integrations/markdoc/src/tokenizer.ts create mode 100644 packages/integrations/markdoc/test/fixtures/render-html/astro.config.mjs create mode 100644 packages/integrations/markdoc/test/fixtures/render-html/markdoc.config.ts create mode 100644 packages/integrations/markdoc/test/fixtures/render-html/package.json create mode 100644 packages/integrations/markdoc/test/fixtures/render-html/src/components/Aside.astro create mode 100644 packages/integrations/markdoc/test/fixtures/render-html/src/components/Mark.astro create mode 100644 packages/integrations/markdoc/test/fixtures/render-html/src/content/blog/components.mdoc create mode 100644 packages/integrations/markdoc/test/fixtures/render-html/src/content/blog/nested-html.mdoc create mode 100644 packages/integrations/markdoc/test/fixtures/render-html/src/content/blog/randomly-cased-html-attributes.mdoc create mode 100644 packages/integrations/markdoc/test/fixtures/render-html/src/content/blog/simple.mdoc create mode 100644 packages/integrations/markdoc/test/fixtures/render-html/src/pages/[slug].astro create mode 100644 packages/integrations/markdoc/test/render-html.test.js diff --git a/.changeset/stupid-poets-grab.md b/.changeset/stupid-poets-grab.md new file mode 100644 index 000000000..47e3005e5 --- /dev/null +++ b/.changeset/stupid-poets-grab.md @@ -0,0 +1,16 @@ +--- +'@astrojs/markdoc': patch +--- + +Adds an "allowHTML" Markdoc integration option. + +When enabled, all HTML in Markdoc files will be processed, including HTML elements within Markdoc tags and nodes. + +Enable this feature in the `markdoc` integration configuration: + +```js +// astro.config.mjs +export default defineConfig({ + integrations: [markdoc({ allowHTML: true })], +}); +``` diff --git a/packages/integrations/markdoc/README.md b/packages/integrations/markdoc/README.md index 15bf24467..d9a7d15c5 100644 --- a/packages/integrations/markdoc/README.md +++ b/packages/integrations/markdoc/README.md @@ -93,7 +93,7 @@ const { Content } = await entry.render(); 📚 See the [Astro Content Collection docs][astro-content-collections] for more information. -## Configuration +## Markdoc config `@astrojs/markdoc` offers configuration options to use all of Markdoc's features and connect UI components to your content. @@ -401,6 +401,36 @@ const { Content } = await entry.render(); This can now be accessed as `$frontmatter` in your Markdoc. +## Integration config options + +The Astro Markdoc integration handles configuring Markdoc options and capabilities that are not available through the `markdoc.config.js` file. + +### allowHTML + +Enables writing HTML markup alongside Markdoc tags and nodes. + +By default, Markdoc will not recognize HTML markup as semantic content. + +To achieve a more Markdown-like experience, where HTML elements can be included alongside your content, set `allowHTML:true` as a `markdoc` integration option. This will enable HTML parsing in Markdoc markup. + + +> **Warning** +> When `allowHTML` is enabled, HTML markup inside Markdoc documents will be rendered as actual HTML elements (including `