-
Notifications
You must be signed in to change notification settings - Fork 68
fix: patch broken numbering definitions #1848
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
Merged
harbournick
merged 6 commits into
main
from
luccas/sd-1647-patch-documents-with-bad-numbering-definition
Jan 28, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6b7af9b
fix: patch broken numbering definitions
bd56e45
test: add no-op test for patching numbering definitions
f7b4249
test: consider multiple missing abstract definitions
7941fe0
Merge branch 'main' into luccas/sd-1647-patch-documents-with-bad-numb…
harbournick c05be5b
chore: fix tests
harbournick fe4a468
Merge branch 'main' into luccas/sd-1647-patch-documents-with-bad-numb…
harbournick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
89 changes: 89 additions & 0 deletions
89
packages/super-editor/src/core/super-converter/v2/importer/patchNumberingDefinitions.js
This file contains hidden or 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,89 @@ | ||
| import { baseOrderedListDef } from '@core/helpers/baseListDefinitions.js'; | ||
|
|
||
| const WORD_2012_NAMESPACE = 'http://schemas.microsoft.com/office/word/2012/wordml'; | ||
|
|
||
| /** | ||
| * Patch numbering definitions in a DOCX file to ensure proper numbering styles. | ||
| * This function modifies the numbering.xml part of the DOCX to fix issues | ||
| * where a numbering definition might target an undefined abstract numbering definition. | ||
| * @param {Object} docx - The DOCX file object to be patched. | ||
| */ | ||
| export function patchNumberingDefinitions(docx) { | ||
| const numberingXml = docx?.['word/numbering.xml']; | ||
| if (!numberingXml) return; | ||
|
|
||
| const numberingRoot = getNumberingRoot(numberingXml); | ||
| if (!numberingRoot?.elements?.length) return; | ||
|
|
||
| const numberingElements = numberingRoot.elements; | ||
|
|
||
| const existingAbstractIds = new Set(); | ||
| for (const el of numberingElements) { | ||
| if (el?.name !== 'w:abstractNum') continue; | ||
| const abstractId = getAbstractIdFromAbstractNode(el); | ||
| if (abstractId) existingAbstractIds.add(abstractId); | ||
| } | ||
|
|
||
| const missingAbstractIds = new Set(); | ||
| for (const el of numberingElements) { | ||
| if (el?.name !== 'w:num') continue; | ||
| const abstractId = getAbstractIdFromNum(el); | ||
| if (!abstractId) continue; | ||
| if (!existingAbstractIds.has(abstractId)) { | ||
| missingAbstractIds.add(abstractId); | ||
| } | ||
| } | ||
|
|
||
| if (!missingAbstractIds.size) return; | ||
|
|
||
| // Ensure the w15 namespace is declared when we add the base ordered list definition, | ||
| // which includes a w15:* attribute. | ||
| numberingRoot.attributes = numberingRoot.attributes || {}; | ||
| if (!numberingRoot.attributes['xmlns:w15']) { | ||
| numberingRoot.attributes['xmlns:w15'] = WORD_2012_NAMESPACE; | ||
| } | ||
|
|
||
| const firstNumIndex = numberingElements.findIndex((el) => el?.name === 'w:num'); | ||
| let insertIndex = firstNumIndex === -1 ? numberingElements.length : firstNumIndex; | ||
|
|
||
| const sortedMissingIds = Array.from(missingAbstractIds).sort((a, b) => { | ||
| const aNum = Number(a); | ||
| const bNum = Number(b); | ||
| const aIsNum = !Number.isNaN(aNum); | ||
| const bIsNum = !Number.isNaN(bNum); | ||
| if (aIsNum && bIsNum) return aNum - bNum; | ||
| if (aIsNum) return -1; | ||
| if (bIsNum) return 1; | ||
| return a.localeCompare(b); | ||
| }); | ||
|
|
||
| for (const abstractId of sortedMissingIds) { | ||
| if (existingAbstractIds.has(abstractId)) continue; | ||
| const newAbstract = deepClone(baseOrderedListDef); | ||
| newAbstract.attributes = { | ||
| ...(newAbstract.attributes || {}), | ||
| 'w:abstractNumId': String(abstractId), | ||
| }; | ||
| numberingElements.splice(insertIndex, 0, newAbstract); | ||
| insertIndex += 1; | ||
| existingAbstractIds.add(abstractId); | ||
| } | ||
| } | ||
|
|
||
| const deepClone = (value) => JSON.parse(JSON.stringify(value)); | ||
|
|
||
| const getNumberingRoot = (numberingXml) => { | ||
| if (!numberingXml?.elements?.length) return null; | ||
| return numberingXml.elements.find((el) => el?.name === 'w:numbering') || numberingXml.elements[0] || null; | ||
| }; | ||
|
|
||
| const getAbstractIdFromNum = (numNode) => { | ||
| const abstractRef = numNode?.elements?.find((child) => child?.name === 'w:abstractNumId'); | ||
| const abstractVal = abstractRef?.attributes?.['w:val']; | ||
| return abstractVal == null ? null : String(abstractVal); | ||
| }; | ||
|
|
||
| const getAbstractIdFromAbstractNode = (abstractNode) => { | ||
| const raw = abstractNode?.attributes?.['w:abstractNumId']; | ||
| return raw == null ? null : String(raw); | ||
| }; |
123 changes: 123 additions & 0 deletions
123
packages/super-editor/src/core/super-converter/v2/importer/patchNumberingDefinitions.test.js
This file contains hidden or 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,123 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { patchNumberingDefinitions } from './patchNumberingDefinitions.js'; | ||
|
|
||
| const makeNumberingXml = (elements) => ({ | ||
| elements: [ | ||
| { | ||
| name: 'w:numbering', | ||
| attributes: { | ||
| 'xmlns:w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main', | ||
| }, | ||
| elements, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| const abstractNum = (id) => ({ | ||
| type: 'element', | ||
| name: 'w:abstractNum', | ||
| attributes: { 'w:abstractNumId': String(id) }, | ||
| elements: [ | ||
| { | ||
| type: 'element', | ||
| name: 'w:lvl', | ||
| attributes: { 'w:ilvl': '0' }, | ||
| elements: [{ type: 'element', name: 'w:numFmt', attributes: { 'w:val': 'decimal' } }], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| const num = ({ numId, abstractId }) => ({ | ||
| type: 'element', | ||
| name: 'w:num', | ||
| attributes: { 'w:numId': String(numId) }, | ||
| elements: [ | ||
| { | ||
| type: 'element', | ||
| name: 'w:abstractNumId', | ||
| attributes: { 'w:val': String(abstractId) }, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| describe('patchNumberingDefinitions', () => { | ||
| it('creates a missing abstractNum using the base ordered list definition', () => { | ||
| const docx = { | ||
| 'word/numbering.xml': makeNumberingXml([ | ||
| abstractNum(41), | ||
| { type: 'comment', comment: 'broken reference follows' }, | ||
| num({ numId: 1, abstractId: 42 }), | ||
| ]), | ||
| }; | ||
|
|
||
| patchNumberingDefinitions(docx); | ||
|
|
||
| const numberingRoot = docx['word/numbering.xml'].elements[0]; | ||
| const numberingElements = numberingRoot.elements; | ||
|
|
||
| const patchedAbstract = numberingElements.find( | ||
| (el) => el?.name === 'w:abstractNum' && String(el.attributes?.['w:abstractNumId']) === '42', | ||
| ); | ||
|
|
||
| expect(patchedAbstract).toBeTruthy(); | ||
| expect(patchedAbstract.elements?.some((el) => el?.name === 'w:lvl')).toBe(true); | ||
| expect(numberingRoot.attributes?.['xmlns:w15']).toBe('http://schemas.microsoft.com/office/word/2012/wordml'); | ||
|
|
||
| const firstNumIndex = numberingElements.findIndex((el) => el?.name === 'w:num'); | ||
| const patchedAbstractIndex = numberingElements.findIndex( | ||
| (el) => el?.name === 'w:abstractNum' && String(el.attributes?.['w:abstractNumId']) === '42', | ||
| ); | ||
| expect(patchedAbstractIndex).toBeGreaterThan(-1); | ||
| expect(firstNumIndex).toBeGreaterThan(patchedAbstractIndex); | ||
| }); | ||
|
|
||
| it('does not change the numbering xml when all abstract references exist', () => { | ||
| const docx = { | ||
| 'word/numbering.xml': makeNumberingXml([ | ||
| abstractNum(41), | ||
| abstractNum(42), | ||
| num({ numId: 1, abstractId: 41 }), | ||
| num({ numId: 2, abstractId: 42 }), | ||
| ]), | ||
| }; | ||
|
|
||
| const before = JSON.parse(JSON.stringify(docx)); | ||
|
|
||
| patchNumberingDefinitions(docx); | ||
|
|
||
| expect(docx).toEqual(before); | ||
| }); | ||
|
|
||
| it('creates multiple missing abstractNum definitions', () => { | ||
| const docx = { | ||
| 'word/numbering.xml': makeNumberingXml([ | ||
| abstractNum(41), | ||
| num({ numId: 1, abstractId: 43 }), | ||
| num({ numId: 2, abstractId: 42 }), | ||
| num({ numId: 3, abstractId: 44 }), | ||
| ]), | ||
| }; | ||
|
|
||
| patchNumberingDefinitions(docx); | ||
|
|
||
| const numberingRoot = docx['word/numbering.xml'].elements[0]; | ||
| const numberingElements = numberingRoot.elements; | ||
|
|
||
| const missingIds = ['42', '43', '44']; | ||
| for (const id of missingIds) { | ||
| const patchedAbstract = numberingElements.find( | ||
| (el) => el?.name === 'w:abstractNum' && String(el.attributes?.['w:abstractNumId']) === id, | ||
| ); | ||
| expect(patchedAbstract).toBeTruthy(); | ||
| } | ||
|
|
||
| const firstNumIndex = numberingElements.findIndex((el) => el?.name === 'w:num'); | ||
| for (const id of missingIds) { | ||
| const patchedAbstractIndex = numberingElements.findIndex( | ||
| (el) => el?.name === 'w:abstractNum' && String(el.attributes?.['w:abstractNumId']) === id, | ||
| ); | ||
| expect(patchedAbstractIndex).toBeGreaterThan(-1); | ||
| expect(firstNumIndex).toBeGreaterThan(patchedAbstractIndex); | ||
| } | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.