Skip to content

Instantly share code, notes, and snippets.

@tclayson
Created January 5, 2021 17:14
Show Gist options
  • Save tclayson/f2b123bdce998e7f4cdda5a5244ca9af to your computer and use it in GitHub Desktop.
Save tclayson/f2b123bdce998e7f4cdda5a5244ca9af to your computer and use it in GitHub Desktop.
import React from "react";
// Import Contentful Rich Text libraries
import { INLINES } from "@contentful/rich-text-types";
import { documentToReactComponents } from "@contentful/rich-text-react-renderer";
function ArticleBodyComponent() {
const references = [];
const options = {
renderNode: {
[INLINES.EMBEDDED_ENTRY]: (node) => {
// Get an index for this reference
const refIdx = references.length;
// Add reference to the array
references.push(node.data.target.fields);
return (
<sup>
<a href={"#ref" + refIdx}>{"[" + refIdx + "]"}</a>
</sup>
);
},
},
};
return (
<div>
{documentToReactComponents(post.body2.json, options)}
{references.length > 0 && (
<>
<h3>References</h3>
<ul>
{references.map((reference, idx) => {
return (
<li key={"ref" + idx}>
<sup>{"[" + idx + "] "}</sup>
<a id={"ref" + idx} href={reference.link["en-US"]}>
{reference.title["en-US"]}
</a>
{" (" + reference.publicationDate["en-US"] + ")"}
</li>
);
})}
</ul>
</>
)}
</div>
);
}
export default ArticleBodyComponent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment