Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dev": "tsc -w"
},
"dependencies": {
"@contentful/rich-text-html-renderer": "^15.11.1",
"@nascentdigital/scribe": "^0.11.2",
"contentful": "^9.1.3",
"contentful-management": "^7.44.2",
Expand Down
13 changes: 11 additions & 2 deletions src/Contentfully.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {IContentfulClient} from './contentful'
import {ContentModel, RichText} from './entities'
import {MediaTransform, QueryOptions} from './QueryOptions'
import {QueryResult} from './QueryResult'

import { documentToHtmlString } from '@contentful/rich-text-html-renderer';

// constants
export const DEFAULT_OPTIONS: Readonly<ContentfullyOptions> = {
Expand Down Expand Up @@ -61,13 +61,15 @@ export class Contentfully {

public readonly contentfulClient: IContentfulClient
public readonly options: Readonly<Partial<ContentfullyOptions>>
public renderRichtext: boolean


public constructor(client: IContentfulClient, options: Readonly<Partial<ContentfullyOptions>> = DEFAULT_OPTIONS) {

// initialize instance variables
this.contentfulClient = client
this.options = options
this.renderRichtext = false
}

public async getEntry<T extends KeyValueMap & ContentModel>(
Expand Down Expand Up @@ -104,6 +106,9 @@ export class Contentfully {
// parse includes
const links = await this._createLinks(entries, multiLocale, options.mediaTransform)

if(options.renderRichtext) {
this.renderRichtext = options.renderRichtext
}
// parse core entries
let items = this._parseEntries(entries.items, links, multiLocale)

Expand Down Expand Up @@ -388,7 +393,7 @@ export class Contentfully {
return this._dereferenceLink(value, links, locale)
}

private _parseRichTextValue(value: EntryFields.RichText, links: any, locale?: string): RichText[] | undefined {
private _parseRichTextValue(value: any, links: any, locale?: string): RichText[] | undefined | string{

// resolve content list
const {content} = value
Expand All @@ -398,6 +403,10 @@ export class Contentfully {
return undefined
}

if(this.renderRichtext) {
return documentToHtmlString(value)
}

return this._parseRichTextContent(content, links, locale)
}

Expand Down
1 change: 1 addition & 0 deletions src/QueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export type MediaTransform = (media: Media) => Promise<Media>
export interface QueryOptions {
mediaTransform?: MediaTransform
flatten?: boolean
renderRichtext?: boolean
}