From b3783893adc58ab6708076edbd2599aaa63ef3c0 Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Wed, 10 Jul 2024 14:22:10 +0530 Subject: [PATCH] fix: updates default node option for reference --- __test__/gql/gql-json-to-html.test.ts | 8 ++--- __test__/json-to-html.test.ts | 11 +++--- __test__/mock/json-element-mock-result.ts | 2 +- __test__/mock/json-element-mock.ts | 38 ++++++++++++++++++++- __test__/reference-to-html.test.ts | 4 +-- src/helper/enumerate-entries.ts | 41 ----------------------- src/options/default-node-options.ts | 17 +++++++++- 7 files changed, 66 insertions(+), 55 deletions(-) diff --git a/__test__/gql/gql-json-to-html.test.ts b/__test__/gql/gql-json-to-html.test.ts index 7a836d4..9627f0a 100644 --- a/__test__/gql/gql-json-to-html.test.ts +++ b/__test__/gql/gql-json-to-html.test.ts @@ -91,8 +91,8 @@ describe('Node parser reference content', () => { GQL.jsonToHTML({entry, paths}) - expect(entry.single_rte).toEqual('
') - expect(entry.multiple_rte).toEqual(['
']) + expect(entry.single_rte).toEqual('\"dummy.pdf\"') + expect(entry.multiple_rte).toEqual(['\"dummy.pdf\"']) done() }) @@ -101,8 +101,8 @@ describe('Node parser reference content', () => { GQL.jsonToHTML({entry, paths}) - expect(entry[0].single_rte).toEqual('
') - expect(entry[0].multiple_rte).toEqual(['
']) + expect(entry[0].single_rte).toEqual('\"dummy.pdf\"') + expect(entry[0].multiple_rte).toEqual(['\"dummy.pdf\"']) done() }) diff --git a/__test__/json-to-html.test.ts b/__test__/json-to-html.test.ts index 2d351ef..51cb753 100644 --- a/__test__/json-to-html.test.ts +++ b/__test__/json-to-html.test.ts @@ -31,7 +31,8 @@ import { unorderListJson2, orderListJson2, testJsonRte, - testJsonAsset} from './mock/json-element-mock' + testJsonAsset, + embeddedAssetAsLinkJsonEntry} from './mock/json-element-mock' import { blockquoteHtml, codeHtml, @@ -116,7 +117,7 @@ describe('Node parser paragraph content', () => { describe('Node parser reference content', () => { - it.skip('Should render reference asset to html from Entry', done => { + it('Should render reference asset to html from Entry', done => { const entry = {...embeddedAssetJsonEntry} jsonToHTML({entry, paths: ['rich_text_editor', 'rte']}) @@ -126,7 +127,7 @@ describe('Node parser reference content', () => { done() }) - it.skip('Should render reference asset to html from Entries', done => { + it('Should render reference asset to html from Entries', done => { const entry = [{ ...embeddedAssetJsonEntry }] jsonToHTML({entry, paths: ['rich_text_editor', 'rte']}) @@ -136,7 +137,7 @@ describe('Node parser reference content', () => { done() }) - it.skip('Should render reference asset to html from Entry with custom render option', done => { + it('Should render reference asset to html from Entry with custom render option', done => { const entry = {...embeddedAssetJsonEntry} jsonToHTML({entry, paths: ['rich_text_editor', 'rte'], renderOption: embeddedAssetWithRenderOption.renderOption}) @@ -146,7 +147,7 @@ describe('Node parser reference content', () => { done() }) - it.skip('Should render reference asset to html from Entries with custom render option', done => { + it('Should render reference asset to html from Entries with custom render option', done => { const entry = [{ ...embeddedAssetJsonEntry }] jsonToHTML({entry, paths: ['rich_text_editor', 'rte'], renderOption: embeddedAssetWithRenderOption.renderOption}) diff --git a/__test__/mock/json-element-mock-result.ts b/__test__/mock/json-element-mock-result.ts index 45ccc9c..6c29bd4 100644 --- a/__test__/mock/json-element-mock-result.ts +++ b/__test__/mock/json-element-mock-result.ts @@ -22,7 +22,7 @@ const classAndIdAttrsHtml = " const styleObjHtml = "

heading1

heading2

heading3

heading4

heading5
heading6
" const referenceObjHtml = "

Embed entry as a link

Open entry as a link in new tab

Bold entry

Bold entry open in new tab

" const referenceObjHtmlBlock = "

Embed entry as a link

Embed entry as a link open in new tab

" -const imagetags = "
\"batman\"
The Batman
" +const imagetags = "
\"batman\"
The Batman
" export { h1Html, h2Html, diff --git a/__test__/mock/json-element-mock.ts b/__test__/mock/json-element-mock.ts index 4d1559a..4d2ea47 100644 --- a/__test__/mock/json-element-mock.ts +++ b/__test__/mock/json-element-mock.ts @@ -1138,6 +1138,41 @@ const paragraphJsonArrayEntry = { } +const embeddedAssetAsLinkJsonEntry = { + uid: 'entry_uid', + rte_data: { + type: 'doc', + attrs: {}, + uid: 'rte_uid', + children: [ + { + type: 'p', + uid: 'p_uid', + attrs: {}, + children: [ + { text: '' }, + { + uid: 'ref_uid', + type: 'reference', + attrs: { + 'display-type': 'link', + type: 'asset', + 'class-name': 'embedded-entry redactor-component undefined-entry', + 'asset-uid': 'asset_uid_2', + 'content-type-uid': 'sys_assets', + target: '_self', + href: 'https://picsum.photos/200' + }, + children: [ { text: 'Door matching', bold: true, underline: true } ] + }, + { text: '' } + ] + } + ], + _version: 4 + } +} + const embeddedAssetJsonEntry = { uid: 'entry_uid', rich_text_editor: {...assetReferenceJson}, @@ -2328,9 +2363,10 @@ export { blockquoteJson, paragraphEntry, unorderListJson, - assetReferenceJson, + assetReferenceJson, paragraphJsonArrayEntry, embeddedAssetJsonEntry, + embeddedAssetAsLinkJsonEntry, embeddedEntryJsonEntry, entryReferenceLinkJson, entryReferenceBlockJson, diff --git a/__test__/reference-to-html.test.ts b/__test__/reference-to-html.test.ts index d6882b2..7fc0293 100644 --- a/__test__/reference-to-html.test.ts +++ b/__test__/reference-to-html.test.ts @@ -152,14 +152,14 @@ describe('Reference Node To HTML', () => { } const resultHTML = referenceToHTML(node, renderOption) - expect(resultHTML).toEqual('
') + expect(resultHTML).toEqual('') done() }) it('Should return image for undefined node asset from default node option', done => { const node = assetReferenceJson.children[0] as unknown as Node const resultHTML = referenceToHTML(node, defaultNodeOption) - expect(resultHTML).toEqual('
') + expect(resultHTML).toEqual('
') done() }) diff --git a/src/helper/enumerate-entries.ts b/src/helper/enumerate-entries.ts index 4d825cb..d6f0906 100644 --- a/src/helper/enumerate-entries.ts +++ b/src/helper/enumerate-entries.ts @@ -77,47 +77,6 @@ export function referenceToHTML( renderOption: RenderOption, renderEmbed?: (metadata: Metadata) => EmbeddedItem | EntryNode, ): string { - if ( node.children!== undefined && node.attrs.type === 'asset' && node.attrs['content-type-uid'] === 'sys_assets') { - - // Extract image information - const src = node.attrs['asset-link']; - const alt = node.attrs?.['redactor-attributes']?.['alt']; - const link = node.attrs.link; - const target = node.attrs.target || ""; - const caption = node.attrs?.['redactor-attributes']?.['asset-caption'] || node.attrs?.['asset-caption'] || ""; - const style = node.attrs.style; - const asset_uid= node.attrs['asset-uid']; - - // Build img tag with optional attributes - let imageTag = `${alt}` + - (link ? `` : "") + - imageTag + - (link ? `` : "") + - (caption ? `
${caption}
` : "") + - ``; - return figureTag; - } - if (node.attrs.type === 'entry' && node.attrs['display-type'] === 'link') { const entryText = node.children ? nodeChildrenToHTML(node.children, renderOption, renderEmbed) : ''; if (node.attrs.target) { diff --git a/src/options/default-node-options.ts b/src/options/default-node-options.ts index 6e10756..24b228d 100644 --- a/src/options/default-node-options.ts +++ b/src/options/default-node-options.ts @@ -105,7 +105,22 @@ export const defaultNodeOption: RenderOption = { ['reference']:(node: Node, next: Next) => { if (node.attrs.type === 'asset') { - return `` + const src = node.attrs['asset-link']; + const alt = node.attrs?.['redactor-attributes']?.['alt']; + const link = node.attrs.link; + const target = node.attrs.target || ""; + const caption = node.attrs?.['redactor-attributes']?.['asset-caption'] || node.attrs?.['asset-caption'] || ""; + const style = node.attrs.style; + const asset_uid= node.attrs['asset-uid']; + + let imageTag = ``; + + return `` + + (link ? `` : "") + + imageTag + + (link ? `` : "") + + (caption ? `
${caption}
` : "") + + ``; } return `` },