-
Notifications
You must be signed in to change notification settings - Fork 130
SD-2440 - fix: toc not loading on paragraphs #2706
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
Changes from 1 commit
ac0874b
7795ec9
5083019
2e6aabd
e4d3419
8e31a4f
a8d623d
d935aa0
139b17c
e4216a3
2c5a073
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -33,16 +33,30 @@ const encode = (params) => { | |||||||||||||||||||
| const { nodes = [], nodeListHandler } = params || {}; | ||||||||||||||||||||
| const node = nodes[0]; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const processedContent = nodeListHandler.handler({ | ||||||||||||||||||||
| let processedContent = nodeListHandler.handler({ | ||||||||||||||||||||
| ...params, | ||||||||||||||||||||
| nodes: node.elements || [], | ||||||||||||||||||||
| }); | ||||||||||||||||||||
| const parentAcceptsBlocks = params?.extraParams?.parentAcceptsBlocks !== false; | ||||||||||||||||||||
| const hasParagraphBlocks = (processedContent || []).some((child) => child?.type === 'paragraph'); | ||||||||||||||||||||
| if (parentAcceptsBlocks && !hasParagraphBlocks) { | ||||||||||||||||||||
| processedContent = [ | ||||||||||||||||||||
| { | ||||||||||||||||||||
| type: 'paragraph', | ||||||||||||||||||||
| content: processedContent.filter((child) => Boolean(child && child.type)), | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| ]; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| const inlineField = !parentAcceptsBlocks; | ||||||||||||||||||||
| const attrs = { | ||||||||||||||||||||
| instruction: node.attributes?.instruction || '', | ||||||||||||||||||||
| }; | ||||||||||||||||||||
| if (!inlineField) { | ||||||||||||||||||||
| attrs.rightAlignPageNumbers = deriveRightAlignPageNumbers(processedContent); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| const processedNode = { | ||||||||||||||||||||
| type: 'tableOfContents', | ||||||||||||||||||||
| attrs: { | ||||||||||||||||||||
| instruction: node.attributes?.instruction || '', | ||||||||||||||||||||
| rightAlignPageNumbers: deriveRightAlignPageNumbers(processedContent), | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| type: inlineField ? 'tableOfContentsInline' : 'tableOfContents', | ||||||||||||||||||||
| attrs, | ||||||||||||||||||||
| content: processedContent, | ||||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -56,8 +70,14 @@ const encode = (params) => { | |||||||||||||||||||
| */ | ||||||||||||||||||||
| const decode = (params) => { | ||||||||||||||||||||
| const { node } = params; | ||||||||||||||||||||
| const isInlineNode = node.type === 'tableOfContentsInline'; | ||||||||||||||||||||
| const tocContent = Array.isArray(node.content) ? node.content : []; | ||||||||||||||||||||
| const contentNodes = tocContent.map((n) => exportSchemaToJson({ ...params, node: n })); | ||||||||||||||||||||
| const inlineContentNodes = tocContent.flatMap((n) => { | ||||||||||||||||||||
| const exported = exportSchemaToJson({ ...params, node: n }); | ||||||||||||||||||||
| if (!exported) return []; | ||||||||||||||||||||
| return Array.isArray(exported) ? exported : [exported]; | ||||||||||||||||||||
| }); | ||||||||||||||||||||
| const blockContentNodes = isInlineNode ? [] : tocContent.map((n) => exportSchemaToJson({ ...params, node: n })); | ||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. two things:
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catches. just fixed the 1st issue. I am now checking the 2nd issue you mentioned, it seems like there's also a problem when exporting the file (the opening in Word). I'll fix it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok I've been trying to fix the export issue for a while now but no luck. I am pretty sure the export issue is unrelated though - after removing the TOC & re-exporting the document, the error persists. If you agree, I'll file a separate ticket to handle it, but in the mean time I'll make sure to fix the issues you mentioned here in the review.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done ✅ |
||||||||||||||||||||
|
|
||||||||||||||||||||
| // Inject the fldChar begin, instrText and fldChar separate into the first child (after any existing pPr) | ||||||||||||||||||||
| const tocBeginElements = [ | ||||||||||||||||||||
|
|
@@ -77,9 +97,16 @@ const decode = (params) => { | |||||||||||||||||||
| }, | ||||||||||||||||||||
| { name: 'w:r', elements: [{ name: 'w:fldChar', attributes: { 'w:fldCharType': 'separate' }, elements: [] }] }, | ||||||||||||||||||||
| ]; | ||||||||||||||||||||
| const tocEndElements = [ | ||||||||||||||||||||
| { name: 'w:r', elements: [{ name: 'w:fldChar', attributes: { 'w:fldCharType': 'end' }, elements: [] }] }, | ||||||||||||||||||||
| ]; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if (isInlineNode) { | ||||||||||||||||||||
| return [...tocBeginElements, ...inlineContentNodes, ...tocEndElements]; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if (contentNodes.length > 0) { | ||||||||||||||||||||
| const firstParagraph = contentNodes[0]; | ||||||||||||||||||||
| if (blockContentNodes.length > 0) { | ||||||||||||||||||||
| const firstParagraph = blockContentNodes[0]; | ||||||||||||||||||||
| let insertIndex = 0; | ||||||||||||||||||||
| if (firstParagraph.elements) { | ||||||||||||||||||||
| const pPrIndex = firstParagraph.elements.findIndex((el) => el.name === 'w:pPr'); | ||||||||||||||||||||
|
|
@@ -91,24 +118,20 @@ const decode = (params) => { | |||||||||||||||||||
| firstParagraph.elements.splice(insertIndex, 0, ...tocBeginElements); | ||||||||||||||||||||
| } else { | ||||||||||||||||||||
| // If there are no paragraphs, create one with the TOC begin elements | ||||||||||||||||||||
| contentNodes.push({ | ||||||||||||||||||||
| blockContentNodes.push({ | ||||||||||||||||||||
| name: 'w:p', | ||||||||||||||||||||
| elements: tocBeginElements, | ||||||||||||||||||||
| }); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Inject the fldChar end into the last child | ||||||||||||||||||||
| const tocEndElements = [ | ||||||||||||||||||||
| { name: 'w:r', elements: [{ name: 'w:fldChar', attributes: { 'w:fldCharType': 'end' }, elements: [] }] }, | ||||||||||||||||||||
| ]; | ||||||||||||||||||||
| const lastParagraph = contentNodes[contentNodes.length - 1]; | ||||||||||||||||||||
| const lastParagraph = blockContentNodes[blockContentNodes.length - 1]; | ||||||||||||||||||||
| if (lastParagraph.elements) { | ||||||||||||||||||||
| lastParagraph.elements.push(...tocEndElements); | ||||||||||||||||||||
| } else { | ||||||||||||||||||||
| lastParagraph.elements = [...tocEndElements]; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return contentNodes; | ||||||||||||||||||||
| return blockContentNodes; | ||||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** @type {import('@translator').NodeTranslatorConfig} */ | ||||||||||||||||||||
|
|
||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.