Skip to content
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

LINK command without brackets throws TypeError: Cannot read properties of undefined (reading 'split') #378

Open
davidjb opened this issue Aug 9, 2024 · 0 comments

Comments

@davidjb
Copy link
Contributor

davidjb commented Aug 9, 2024

A common typo I've encountered from user templates is with the LINK command, where a user misses the brackets in the command's options:

// Was
{{ link {url: 'https://example.com'} }}

// Should be
{{ link ({url: 'https://example.com'}) }}

If the surrounding () happens to be missed, an exception gets thrown when calling createReport:

node_modules/docx-templates/lib/xml.js:90
    var segments = str.split(options.literalXmlDelimiter);
                       ^
TypeError: Cannot read properties of undefined (reading 'split')
    at sanitizeText (node_modules/docx-templates/lib/xml.js:90:24)
    at buildXml (node_modules/docx-templates/lib/xml.js:65:16)
    at node_modules/docx-templates/lib/xml.js:77:20
    at Array.forEach (<anonymous>)
    at buildXml (node_modules/docx-templates/lib/xml.js:76:24)
    at node_modules/docx-templates/lib/xml.js:77:20
    at Array.forEach (<anonymous>)
    at buildXml (node_modules/docx-templates/lib/xml.js:76:24)
    at node_modules/docx-templates/lib/xml.js:77:20
    at Array.forEach (<anonymous>)

This is invalid syntax according the docx-templates docs but it's an easy mistake to overlook and it's a hard failure and even with failFast: false gets thrown separately to other in-document errors. Ideally the failure would be softer, like how missing a mismatched or missing {{ end-for }} command occurs and not throw an unhandled exception, being incorporated into other document errors.

Otherwise, if feasible, avoid requiring the brackets in the syntax would avoid them being forgotten.


When reporting a bug, please provide:

  • A small reproducible example consisting of the code and .docx template you used.

Code:

import { readFile } from 'node:fs/promises';
import { createReport } from 'docx-templates';

createReport({
  cmdDelimiter: ['{{', '}}'],
  failFast: false,
  template: readFile('template.docx'),
});

template.docx:

{{ link {url: 'https://example.com'} }}
  • A screenshot of the result you get and an explanation as to why this result is unexpected: See above
  • Information about your compiler toolchain version (e.g. Angular, Webpack, etc.): Direct usage in Node
  • Information about your runtime environment (which browser, Electron, or NodeJS version):
    Node, docx-template 4.13.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant