Skip to content

Bug: blog RSS feed generation failures with MDX posts #5664

Closed

Description

@slorber

🐛 Bug Report

With #4330, we now render the full blog posts in the RSS feed, as HTML.

The way we currently do it is "good enough" but has several limitations:

MDX components replaced by divs

Using this MDX:

# title

import SomeCompent from "@site/src/someComponent"

<SomeCompent>Hello</SomeCompent>

Will render as:

<h1>title</h1>

<div>Hello</div>

The logic of <SomeComponent> is never evaluated.

For layout/wrapper components, it's not a big deal as the content remains in the RSS feed, but in some cases, it can be worst.

Not having a global MDX config and giving themes the ability to "register" some MDX comps and scope can help solve this problem. Related to #4625

No ```mdx-code-block support

```mdx-code-block is an escape hatch we added so that external tools (like Crowdin) do not mess-up with unsupported MDX markup.

The MDX content in these code blocks must be evaluated, as if it was not a code block

No relative image path support

MD referencing co-located assets such as:

![Docusaurus beta party](./img/image_cropped.png)

will be transformed as

<img src="./img/image_cropped.png" alt="Docusaurus beta party"/>

Unfortunately, the image is not hosted at ./img/image_cropped.png in the production site due to using Webpack's file-loader.

The real image URL in prod is /assets/images/image_cropped-2744c01b1fc0b451dbdb65a1d568fbff.png

We need a way to ensure such MD image declarations work in the RSS feed properly (ideally without changing the existing image asset URL or duplicating the asset in final site build)

Activity

  1. added
    bug
    status: needs triage
    on Oct 8, 2021
  2. Josh-Cena commented on Oct 16, 2021

    @Josh-Cena
    Collaborator

    Regressions that make sites fail to build at all:

    • styles is not defined when import styles from './styles.module.css';
    • require is not defined when require('./assets/img.png').default.

    The missing Webpack infra is more severe than missing MDX features

  3. changed the title [-]Bug: fix Docusaurus blog RSS feed limitations[/-] [+]Bug: blog RSS feed generation failures with MDX posts[/+] on Oct 20, 2021
  4. slorber commented on Oct 20, 2021

    @slorber
    CollaboratorAuthor

    In case of MDX to HTML conversion failures, generally due to require() calls, I added a quickfix to temporarily swallow errors here: #5753

    This is not a big deal because posts without HTML content are still useful in the RSS feed (behavior we had before). I'm not sure how this problem must be solved so I didn't want to introduce a new temporary API for this.


    @wooorm any insight to share on how to render an MDX file (including JSX tags/imports/exports + require("./img.png") to a RSS feed HTML string?

    We have something like that currently but it's obviously not ideal:

    import React from 'react';
    import ReactDOMServer from 'react-dom/server';
    import MDX from '@mdx-js/runtime';
    import removeImports from 'remark-mdx-remove-imports';
    import removeExports from 'remark-mdx-remove-exports';
    
    export function mdxToHtml(
      mdxStr: string,
    ): string {
      return ReactDOMServer.renderToString(
        React.createElement(MDX, {remarkPlugins: [removeImports, removeExports]}, [
          mdxStr,
        ]),
      );
    }

    Of all the popular MDX-based blogs, I couldn't find one where the RSS feed renders full HTML content.

  5. wooorm commented on Oct 21, 2021

    @wooorm
    Contributor

    I’m doing this as a post step on the generated HTML. As in, I first generate the site. Then I take the generated HTML of recent blog posts. And turn those into a feed:

    https://github.com/mdx-js/mdx/blob/7d09ef8266ffffb089d583363e16db76e841bec1/website/post.js#L48-L110

    Assuming there’s some static generation, then at some point, you already have a component for the MDX page?

  6. slorber commented on Oct 21, 2021

    @slorber
    CollaboratorAuthor

    Thanks @wooorm, that was also one idea I had, and yes when we generate the blog feed the HTML files are already generated so we can definitively read them here.

    I found this idea a bit hacky (likely to break due to generated filenames or HTML structure), but it can probably be good enough for now until we find better 🤪

  7. wooorm commented on Oct 21, 2021

    @wooorm
    Contributor

    You do need to have the metadata about blog posts again, and where they were rendered. And a class or ID on the body of the text. But I don't really find it hack if it's then very close to what instapaper or safari reader or so do, too.
    Alnternatively: do it when you render the content into the body. Perhaps render it twice, once with a "this is for the static website" and once with "this is for the static feed", so components can switch and return different markup for each "media query"?

  8. removed
    status: needs triage
    on Oct 29, 2021
  9. added this to the 2.0.0 GA milestone on Nov 3, 2021
  10. modified the milestones: 2.0.0, 3.0.0 on Dec 16, 2021
  11. Josh-Cena commented on Dec 28, 2021

    @Josh-Cena
    Collaborator

    Not so critical bug: #6195

  12. modified the milestones: 3.0.0, 2.0.0 on Apr 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions