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

feat: structured data faqs - not fully working; basis for discussion #8954

Closed
wants to merge 2 commits into from

Conversation

johnnyreilly
Copy link
Contributor

Pre-flight checklist

  • I have read the Contributing Guidelines on pull requests.
  • If this is a code change: I have written unit tests and/or added dogfooding pages to fully verify the new behavior.
  • [] If this is a new API or substantial change: the PR has an accompanying issue (closes #0000) and the maintainers have approved on my working plan.

Motivation

I previously built a Structured Data FAQs plugin: https://johnnyreilly.com/docusaurus-structured-data-faqs-mdx

After I did that I discovered discussion of it as a feature here: https://docusaurus.io/feature-requests/p/creation-of-structured-faq

@Josh-Cena had suggested it would be good to have this built in. I started hacking together something to do that. This approach parses MD(X) files looking for ## FAQs and uses child headings and their answers to construct structured data FAQs as well.

This PR is that. There's a number of issues:

  1. When running locally the plugin I've built is invoked multiple times - the FAQs are picked up in all initial runs but vanish in the final run - I'm mystified as to what is happening here. Any experts on remark plugins being invoked multiple times, but with different inputs on subsequent runs? Weird.
  2. I'm not totally sure that, I've cracked how to pass through values from an remark plugin to the Docusaurus blog loader. I sort of copied the TOC approach. It doesn't seem to quite work.
  3. Is this a good API design anyway? Would perhaps having FAQs as part of Frontmatter be better instead? Maybe Structured Data FAQs aren't that important anyways.

This PR is a (not quite) POC - and before I advance it further, I wanted to see if people saw value in it. Maybe not. If they do, I'd appreciate some pointers on solving the problems.

Also happy for this to be closed - maybe this isn't a good direction.

Test Plan

Test links

Deploy preview: https://deploy-preview-_____--docusaurus-2.netlify.app/

Related issues/PRs

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label May 1, 2023
Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand everything yet but that looks a bit complicated and opinionated API to me.

Is this need so common to justify the creation of a core remark plugin, enabled by default?
Couldn't this benefit other MDX-based sites as well (like Next.js/Astro?). I feel it does not really belong in Docusaurus core.

Couldn't we use a theme-common helper component for those that want to create a FAQ? Your solution here looks much simpler: https://johnnyreilly.com/docusaurus-structured-data-faqs-mdx#the-faqstructureddata-mdx-component

// faqs questions are headings

const value = toString(child);
if (child.depth === 2 && value === 'FAQs') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks quite opinionated to use hardcoded values like this and probably does not support i18n where headings will be translated

console.log(x, 'faqsNodes', vfile.path, faqsNodes, faqs, root);
console.log('faqsNodes', vfile.path, faqs);
}
children[targetIndex]!.value = `export const ${name} = ${stringifyObject(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not look like MDX v2, we merged the PR so the TOC code you got inspired from is probably very different now 🤪

@johnnyreilly
Copy link
Contributor Author

There more I think about it, the less I'm convinced this is a great idea / implementation. I think I'm going to close this as a consequence.

@johnnyreilly
Copy link
Contributor Author

Couldn't we use a theme-common helper component for those that want to create a FAQ? Your solution here looks much simpler: https://johnnyreilly.com/docusaurus-structured-data-faqs-mdx#the-faqstructureddata-mdx-component

I think this is a good idea @slorber. I'd be happy to contribute the FAQs component to Docusaurus as a shared component if that would be helpful?

@slorber
Copy link
Collaborator

slorber commented May 30, 2023

Yes why not

Was wondering if it wouldn't be simpler to add the structured data directly to the html elements instead of adding extra scripts?

Like we do for blog posts:

export default function BlogPostItemContainer({
  children,
  className,
}: Props): JSX.Element {
  const {frontMatter, assets} = useBlogPost();
  const {withBaseUrl} = useBaseUrlUtils();
  const image = assets.image ?? frontMatter.image;
  return (
    <article
      className={className}
      itemProp="blogPost"
      itemScope
      itemType="http://schema.org/BlogPosting">
      {image && (
        <meta itemProp="image" content={withBaseUrl(image, {absolute: true})} />
      )}
      {children}
    </article>
  );
}

On the usage API side, how would we use the FAQ components?

With Docusauurs v3 / MDX 2 there's a new CommonMark mode (#3018) and it would be nice if this feature can be used by both .md and .mdx files.

I think we have 2 choices then:

  • automatically register a <faq> component in the MDX provider
  • use a Remark directive:
:::faq[My FAQ Title]

My FAQ Content

::: 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants