-
Notifications
You must be signed in to change notification settings - Fork 127
feature: super validator infrastructure and first two validators #725
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
721b476
feat: super validator infrastructure and first two validators
harbournick 7ccfe14
chore: clean up
harbournick 453f2c1
fix: jsdoc for image validator
harbournick 574000b
feat: add 100% test coverage to validator, change directory structure…
harbournick fb91661
chore: fix spelling
harbournick cdd2692
chore: add safety checks to docx helpers and 100% tests for document …
harbournick c337647
fix: add missing id generation call to image validator, fix tests for…
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
157 changes: 157 additions & 0 deletions
157
packages/super-editor/src/core/super-converter/docx-helpers/document-rels.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,157 @@ | ||
| // @ts-check | ||
| import { RELATIONSHIP_TYPES } from './docx-constants.js'; | ||
|
|
||
| /** @typedef {import('../types.js').Editor} Editor */ | ||
| /** @typedef {import('../types.js').XmlRelationshipElement} XmlRelationshipElement */ | ||
| /** @typedef {import('../types.js').RelationshipType} RelationshipType */ | ||
|
|
||
| /** | ||
| * Get all relationship elements from the document.xml.rels. | ||
| * @param {Editor} editor The editor instance | ||
| * @returns {XmlRelationshipElement[]} An array of relationship elements | ||
| */ | ||
| export const getDocumentRelationshipElements = (editor) => { | ||
| const docx = editor.converter?.convertedXml; | ||
| if (!docx) return []; | ||
|
|
||
| const documentRels = docx['word/_rels/document.xml.rels']; | ||
| const elements = documentRels?.elements; | ||
| if (!Array.isArray(elements)) return []; | ||
|
|
||
| const relationshipTag = elements.find((el) => el.name === 'Relationships'); | ||
| return relationshipTag?.elements || []; | ||
| }; | ||
|
|
||
| /** | ||
| * Get the maximum relationship ID from existing relationships. | ||
| * @param {XmlRelationshipElement[]} relationships The array of relationship elements | ||
| * @returns {number} The maximum relationship ID integer | ||
| */ | ||
| export const getMaxRelationshipIdInt = (relationships) => { | ||
| const ids = []; | ||
| relationships.forEach((rel) => { | ||
| const splitId = rel.attributes.Id.split('rId'); | ||
| const parsedInt = parseInt(splitId[1], 10); | ||
| if (Number.isInteger(parsedInt)) { | ||
| ids.push(parsedInt); | ||
| } | ||
| }); | ||
|
|
||
| if (ids.length === 0) return 0; | ||
| return Math.max(...ids); | ||
| }; | ||
|
|
||
| /** | ||
| * Find an existing relationship ID based on the target path. | ||
| * @param {string} target The target path to search for | ||
| * @param {Editor} editor The editor instance | ||
| * @returns {string|null} The relationship ID if found, otherwise null | ||
| */ | ||
| export const findRelationshipIdFromTarget = (target, editor) => { | ||
| if (!target) return null; | ||
|
|
||
| if (target.startsWith('word/')) target = target.replace('word/', ''); | ||
| const relationships = getDocumentRelationshipElements(editor); | ||
| const existingLinkRel = relationships?.find((rel) => rel.attributes.Target === target); | ||
| if (existingLinkRel) { | ||
| return existingLinkRel.attributes.Id; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Insert a new relationship into the document.xml.rels. | ||
| * This will verify that we do not already have a relationship for the target. | ||
| * If a relationship already exists, it will not create a new one. | ||
| * @param {string} target The target path for the relationship | ||
| * @param {RelationshipType} type The type of the relationship | ||
| * @param {Editor} editor The editor instance | ||
| * @returns {string|null} The new or existing relationship ID or null if it could not be created | ||
| * @throws {Error} When required parameters are missing or invalid | ||
| */ | ||
| export const insertNewRelationship = (target, type, editor) => { | ||
| // Input validation | ||
| if (!target || typeof target !== 'string') { | ||
| throw new Error('Target must be a non-empty string'); | ||
| } | ||
| if (!type || typeof type !== 'string') { | ||
| throw new Error('Type must be a non-empty string'); | ||
| } | ||
| if (!editor) { | ||
| throw new Error('Editor instance is required'); | ||
| } | ||
|
|
||
| // Check if relationship type is supported | ||
| const mappedType = RELATIONSHIP_TYPES[type]; | ||
| if (!mappedType) { | ||
| console.warn( | ||
| `Unsupported relationship type: ${type}. Available types: ${Object.keys(RELATIONSHIP_TYPES).join(', ')}`, | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| // Check for existing relationship | ||
| const existingRelId = findRelationshipIdFromTarget(target, editor); | ||
| if (existingRelId) { | ||
| console.info(`Reusing existing relationship for target: ${target} (ID: ${existingRelId})`); | ||
| return existingRelId; | ||
| } | ||
|
|
||
| // Validate document structure | ||
| const docx = editor.converter?.convertedXml; | ||
| if (!docx) { | ||
| console.error('No converted XML found in editor'); | ||
| return null; | ||
| } | ||
|
|
||
| const documentRels = docx['word/_rels/document.xml.rels']; | ||
| if (!documentRels) { | ||
| console.error('No document relationships found in the docx'); | ||
| return null; | ||
| } | ||
|
|
||
| const relationshipsTag = documentRels.elements?.find((el) => el.name === 'Relationships'); | ||
| if (!relationshipsTag) { | ||
| console.error('No Relationships tag found in document relationships'); | ||
| return null; | ||
| } | ||
|
|
||
| // Ensure elements array exists | ||
| if (!relationshipsTag.elements) { | ||
| relationshipsTag.elements = []; | ||
| } | ||
|
|
||
| // Generate new relationship ID | ||
| const newId = getNewRelationshipId(editor); | ||
| if (!newId) { | ||
| console.error('Failed to generate new relationship ID'); | ||
| return null; | ||
| } | ||
|
|
||
| // Create new relationship element | ||
| const newRel = { | ||
| type: 'element', | ||
| name: 'Relationship', | ||
| attributes: { | ||
| Id: newId, | ||
| Type: mappedType, | ||
| Target: target, | ||
| }, | ||
| }; | ||
|
|
||
| // Insert the new relationship | ||
| relationshipsTag.elements.push(newRel); | ||
|
|
||
| return newId; | ||
| }; | ||
|
|
||
| /** | ||
| * Generate a new relationship ID for the document. | ||
| * This will be in the format rIdX where X is the next available integer. | ||
| * @param {Editor} editor The editor instance | ||
| * @returns {string} The new relationship ID | ||
| */ | ||
| export const getNewRelationshipId = (editor) => { | ||
| const relationships = getDocumentRelationshipElements(editor); | ||
| const maxIdInt = getMaxRelationshipIdInt(relationships); | ||
| return `rId${maxIdInt + 1}`; | ||
| }; | ||
Oops, something went wrong.
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.