Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
slorber opened this issue Oct 8, 2021 · 6 comments · Fixed by #6454
Closed

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

slorber opened this issue Oct 8, 2021 · 6 comments · Fixed by #6454
Labels
bug An error in the Docusaurus core causing instability or issues with its execution
Milestone

Comments

@slorber
Copy link
Collaborator

slorber commented Oct 8, 2021

🐛 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)

@slorber slorber added bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Oct 8, 2021
@Josh-Cena
Copy link
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

@slorber
Copy link
Collaborator Author

slorber commented Oct 20, 2021

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.

@wooorm
Copy link
Contributor

wooorm commented Oct 21, 2021

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?

@slorber
Copy link
Collaborator Author

slorber commented Oct 21, 2021

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 🤪

@wooorm
Copy link
Contributor

wooorm commented Oct 21, 2021

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"?

@Josh-Cena Josh-Cena removed the status: needs triage This issue has not been triaged by maintainers label Oct 29, 2021
@Josh-Cena Josh-Cena added this to the 2.0.0 GA milestone Nov 3, 2021
@slorber slorber modified the milestones: 2.0.0, 3.0.0 Dec 16, 2021
@Josh-Cena
Copy link
Collaborator

Not so critical bug: #6195

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An error in the Docusaurus core causing instability or issues with its execution
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants