open-xml-templating/docxtemplater

The parser parses '()' incorrectly in case of using custom delimiters like this '$(', ')'

svirkoei opened this issue · 6 comments

Environment

  • Version of docxtemplater : 3.39.1 (not reproducible in 3.22.8)
  • Used docxtemplater-modules : no special
  • Runner : nodejs v16.19.0

How to reproduce my problem :

The parser parses '()' incorrectly in case of using custom delimiters like this '$(', ')' and produce the error 'The tag beginning with ") Some tex" is unopened'. Meanwhile, in v3.22.8 such docs are parsed correctly (as in the expected result)

My template is

$(last_name) $(first_name) 


Some text (Some text)  Some text


$(last_name) $(first_name) 

I would expect it to :

Hipp Edgar

Some text (Some text)  Some text

Hipp Edgar

Interesting issue, indeed, it has been decided a few releases ago to show all kind of errors when there are delimiters that are "siblingless", eg they have no sibling delimiter.

In your example, the end delimiter at "(Some text)" has no starting delimiter matching it.

I'm open to add an option that would say that you want to ignore those type of errors, I have to think about the API for this new option.

Maybe something like this (this is a suggestion, and is not yet present in docxtemplater) :

Option 1:

const doc = new Docxtemplater(zip, {
    errors: {
        allowUnopenedTag: true,
    }
});

Option 2:

const doc = new Docxtemplater(zip, {
      allowUnopenedTag: true,
});

Option 3:

const doc = new Docxtemplater(zip, {
    errors: {
        ignoreUnopened: true,
    }
});

I would choose the first option. Thanks for replying fast

We will be implementing the feature with the syntax suggested in Option 1.

We will instead be using following option configuration :

const doc = new Docxtemplater(zip, {
    syntax: {
        allowUnopenedTag: true,
    }
});

Because "errors" is a bit misleading.

Fixed in v3.40.0, which was just released today !

This is opt-in, you have to write :

new Docxtemplater(zip, {
  paragraphLoop: true,
  linebreaks: true,

  syntax: {
    allowUnopenedTag: true,
  },
  delimiters: {
     start: '$(',
     end: ')',
  }
});