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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, it, expect } from 'vitest';
import {
collapseWhitespaceNextToInlinePassthrough,
defaultNodeListHandler,
filterOutRootInlineNodes,
normalizeTableBookmarksInContent,
} from './docxImporter.js';
Expand Down Expand Up @@ -324,3 +325,162 @@ describe('normalizeTableBookmarksInContent', () => {
expect(innerCellParagraphContent[2]).toMatchObject({ type: 'bookmarkEnd', attrs: { id: 'n1' } });
});
});

describe('docPartObj paragraph import regression', () => {
const createEditorStub = () => ({
schema: {
nodes: {
run: { isInline: true, spec: { group: 'inline' } },
documentPartObject: { isInline: false, spec: { group: 'block' } },
},
},
});

it('hoists a docPartObj SDT out of paragraph inline content', () => {
const nodeListHandler = defaultNodeListHandler();
const paragraphNode = {
name: 'w:p',
attributes: { 'w:rsidRDefault': 'AAA111' },
elements: [
{
name: 'w:sdt',
elements: [
{
name: 'w:sdtPr',
elements: [
{ name: 'w:id', attributes: { 'w:val': '123456789' } },
{
name: 'w:docPartObj',
elements: [
{ name: 'w:docPartGallery', attributes: { 'w:val': 'Table of Figures' } },
{ name: 'w:docPartUnique' },
],
},
],
},
{
name: 'w:sdtContent',
elements: [
{
name: 'w:p',
attributes: { 'w14:paraId': '11111111', 'w14:textId': '11111111' },
elements: [
{
name: 'w:r',
elements: [{ name: 'w:t', elements: [{ type: 'text', text: 'Table of Figures' }] }],
},
],
},
{
name: 'w:p',
attributes: { 'w14:paraId': '22222222', 'w14:textId': '22222222' },
elements: [
{ name: 'w:r', elements: [{ name: 'w:t', elements: [{ type: 'text', text: 'Figure 1' }] }] },
{ name: 'w:r', elements: [{ name: 'w:tab' }] },
{ name: 'w:r', elements: [{ name: 'w:t', elements: [{ type: 'text', text: '1' }] }] },
],
},
],
},
],
},
],
};

const result = nodeListHandler.handler({
nodes: [paragraphNode],
docx: {},
editor: createEditorStub(),
path: [],
});

expect(result).toHaveLength(1);
expect(result[0].type).toBe('documentPartObject');
expect(result[0].attrs).toMatchObject({
id: '123456789',
docPartGallery: 'Table of Figures',
docPartUnique: true,
});
expect(result[0].content).toHaveLength(2);
expect(result[0].content[0].type).toBe('paragraph');
expect(result[0].content[1].type).toBe('paragraph');
});

it('splits inline text around a docPartObj SDT into sibling paragraphs', () => {
const nodeListHandler = defaultNodeListHandler();
const paragraphNode = {
name: 'w:p',
attributes: { 'w:rsidRDefault': 'BBB222' },
elements: [
{ name: 'w:r', elements: [{ name: 'w:t', elements: [{ type: 'text', text: 'Before' }] }] },
{
name: 'w:sdt',
elements: [
{
name: 'w:sdtPr',
elements: [
{ name: 'w:id', attributes: { 'w:val': '123456789' } },
{
name: 'w:docPartObj',
elements: [{ name: 'w:docPartGallery', attributes: { 'w:val': 'Table of Figures' } }],
},
],
},
{
name: 'w:sdtContent',
elements: [
{
name: 'w:p',
elements: [
{ name: 'w:r', elements: [{ name: 'w:t', elements: [{ type: 'text', text: 'Figure 1' }] }] },
],
},
],
},
],
},
{ name: 'w:r', elements: [{ name: 'w:t', elements: [{ type: 'text', text: 'After' }] }] },
],
};

const result = nodeListHandler.handler({
nodes: [paragraphNode],
docx: {},
editor: createEditorStub(),
path: [],
});

expect(result).toHaveLength(3);
expect(result[0].type).toBe('paragraph');
expect(result[0].content?.[0]?.type).toBe('run');
expect(result[0].content?.[0]?.content?.[0]).toMatchObject({ type: 'text', text: 'Before' });
expect(result[1]).toMatchObject({
type: 'documentPartObject',
attrs: { id: '123456789', docPartGallery: 'Table of Figures' },
});
expect(result[2].type).toBe('paragraph');
expect(result[2].content?.[0]?.type).toBe('run');
expect(result[2].content?.[0]?.content?.[0]).toMatchObject({ type: 'text', text: 'After' });
});

it('keeps normal paragraphs intact when schema metadata is unavailable', () => {
const nodeListHandler = defaultNodeListHandler();
const paragraphNode = {
name: 'w:p',
attributes: { 'w:rsidRDefault': 'CCC333' },
elements: [{ name: 'w:r', elements: [{ name: 'w:t', elements: [{ type: 'text', text: 'Header text' }] }] }],
};

const result = nodeListHandler.handler({
nodes: [paragraphNode],
docx: {},
editor: {},
path: [],
});

expect(result).toHaveLength(1);
expect(result[0].type).toBe('paragraph');
expect(result[0].content?.[0]?.type).toBe('run');
expect(result[0].content?.[0]?.content?.[0]).toMatchObject({ type: 'text', text: 'Header text' });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const handleParagraphNode = (params) => {
return { nodes: [], consumed: 0 };
}
const schemaNode = wPNodeTranslator.encode(params);
const newNodes = schemaNode ? [schemaNode] : [];
const newNodes = Array.isArray(schemaNode) ? schemaNode : schemaNode ? [schemaNode] : [];
return { nodes: newNodes, consumed: 1 };
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Determine whether a translated PM JSON node should be treated as inline.
*
* Falls back to known inline leaf types when schema metadata is unavailable.
*
* @param {unknown} node
* @param {import('prosemirror-model').Schema | undefined} schema
* @returns {boolean}
*/
const INLINE_FALLBACK_TYPES = new Set([
'text',
'run',
'bookmarkStart',
'bookmarkEnd',
'tab',
'lineBreak',
'hardBreak',
'commentRangeStart',
'commentRangeEnd',
'commentReference',
'permStart',
'permEnd',
'footnoteReference',
'endnoteReference',
'fieldAnnotation',
'structuredContent',
'passthroughInline',
'page-number',
'total-page-number',
'pageReference',
'crossReference',
'citation',
'authorityEntry',
'sequenceField',
'indexEntry',
'tableOfContentsEntry',
]);

export function isInlineNode(node, schema) {
if (!node || typeof node !== 'object' || typeof node.type !== 'string') return false;

const nodeType = schema?.nodes?.[node.type];
if (nodeType) {
if (typeof nodeType.isInline === 'boolean') return nodeType.isInline;
if (nodeType.spec?.group && typeof nodeType.spec.group === 'string') {
return nodeType.spec.group.split(' ').includes('inline');
}
}

return INLINE_FALLBACK_TYPES.has(node.type);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, expect, it } from 'vitest';
import { isInlineNode } from './is-inline-node.js';

describe('isInlineNode', () => {
it('treats common importer inline nodes as inline without schema metadata', () => {
expect(isInlineNode({ type: 'text', text: 'x' })).toBe(true);
expect(isInlineNode({ type: 'run', content: [] })).toBe(true);
expect(isInlineNode({ type: 'bookmarkStart', attrs: { id: '1' } })).toBe(true);
expect(isInlineNode({ type: 'bookmarkEnd', attrs: { id: '1' } })).toBe(true);
expect(isInlineNode({ type: 'tab' })).toBe(true);
expect(isInlineNode({ type: 'footnoteReference', attrs: { id: '1' } })).toBe(true);
});

it('uses nodeType.isInline when available', () => {
const schema = {
nodes: {
mention: { isInline: true, spec: {} },
table: { isInline: false, spec: {} },
},
};

expect(isInlineNode({ type: 'mention', attrs: { id: 'm1' } }, schema)).toBe(true);
expect(isInlineNode({ type: 'table', content: [] }, schema)).toBe(false);
});

it('falls back to schema group metadata when isInline is unavailable', () => {
const schema = {
nodes: {
customInline: { spec: { group: 'inline custom-inline' } },
customBlock: { spec: { group: 'block' } },
},
};

expect(isInlineNode({ type: 'customInline' }, schema)).toBe(true);
expect(isInlineNode({ type: 'customBlock' }, schema)).toBe(false);
});

it('returns false for missing or unknown node types', () => {
expect(isInlineNode(null)).toBe(false);
expect(isInlineNode({})).toBe(false);
expect(isInlineNode({ type: 'unknownNode' }, { nodes: {} })).toBe(false);
});
});
Loading
Loading