-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Rich Text implementation in gatsby-source-contentful (#25249)
* feat(contentful): improve Rich Text field support * Allow circular references * Improve performance and reduce RAM footprint * Query referenced entries and assets via GraphQL fixes #24221 BREAKING CHANGE: * Entities references in Rich Text fields are no more automatically resolved * Use the `raw` subfield instead of `json` * Use GraphQL to define your referenced data with the new `references` field * Removes the `resolveFieldLocales` as the new `references` field automatically resolves locales * To render Rich Text fields unse the new `renderRichText()` function from `gatsby-source-contentful/rich-text` * perf(gatsby-source-contentful): use getNodesByType to locate Rich Text references * clean up from rebase * refactor to use a query for entry references * simplify and reduce complexity of rich text raw field value injection * performance: directly inject references into Rich Text field, skipping the extra node type * remove plugin config for no more existing rich text resolveFieldLocales * add hint to remove extra traversal of rich text as soon fixIds is gone * add first test for rich text * clean up code and use more Maps * align readme for new rich text changes * remove workarounds for fixIds * add typescript types for rich text render function * add tests for rich text rendering * Dirty lock file Co-authored-by: Peter van der Zee <github-public@qfox.nl> Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
- Loading branch information
1 parent
631f3c4
commit a256346
Showing
15 changed files
with
1,968 additions
and
629 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module "gatsby-source-contentful" {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ReactNode } from "react" | ||
import { Options } from "@contentful/rich-text-react-renderer" | ||
|
||
interface ContentfulRichTextGatsbyReference { | ||
/** | ||
* Either ContentfulAsset for assets or ContentfulYourContentTypeName for content types | ||
*/ | ||
__typename: string | ||
contentful_id: string | ||
} | ||
|
||
interface RenderRichTextData<T extends ContentfulRichTextGatsbyReference> { | ||
raw: string | ||
references: T[] | ||
} | ||
|
||
export function renderRichText< | ||
TReference extends ContentfulRichTextGatsbyReference | ||
>(data: RenderRichTextData<TReference>, options?: Options): ReactNode |
35 changes: 35 additions & 0 deletions
35
packages/gatsby-source-contentful/scripts/generate-fetch-fixtures.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Helper script to generate the test fixtures for the contentfulFetch call | ||
|
||
import { writeFileSync } from "fs" | ||
import { resolve } from "path" | ||
|
||
import fetch from "gatsby-source-contentful/src/fetch" | ||
|
||
let syncToken = `` | ||
const reporter = { | ||
verbose: console.log, | ||
info: console.info, | ||
panic: console.error, | ||
} | ||
|
||
const generateFixutures = async () => { | ||
const pluginConfig = new Map([ | ||
[`spaceId`, `ahntqop9oi7x`], | ||
[`accessToken`, `JW5Rm2ZoQl6eoqxcTC1b0J_4eUmXoBljtY9aLGRhRYw`], | ||
[`environment`, `master`], | ||
]) | ||
pluginConfig.getOriginalPluginOptions = () => { | ||
return {} | ||
} | ||
|
||
const result = await fetch({ syncToken, reporter, pluginConfig }) | ||
|
||
writeFileSync( | ||
resolve(__dirname, `fetch-result.json`), | ||
JSON.stringify(result, null, 2) | ||
) | ||
|
||
console.log(`Updated result json`) | ||
} | ||
|
||
generateFixutures() |
Oops, something went wrong.