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

Impossible to multiple patches of a key in a same paragraph #2267

Closed
Suniron opened this issue Jul 25, 2023 · 8 comments · Fixed by #2497
Closed

Impossible to multiple patches of a key in a same paragraph #2267

Suniron opened this issue Jul 25, 2023 · 8 comments · Fixed by #2497
Labels

Comments

@Suniron
Copy link

Suniron commented Jul 25, 2023

Hey there!

I can't do multiple patches of the same key in one sentence.

For example, in the following text, only the first key will be patched:

The company {{company_name}}, is {{company_name}}

I'm using the code above to patch my docx files:

import {
  PatchType,
  TextRun,
  patchDocument,
} from 'docx'

const patchedDocxTemplate = await patchDocument(baseDocxTemplate, {
   patches: {
    company_name: {
      children: [new TextRun('MY_COMPANY')],
      type: PatchType.PARAGRAPH,
    },
  }
})
@Suniron Suniron changed the title Impossible to multiple patches of a key in one sentence Impossible to multiple patches of a key in a same paragraph Jul 25, 2023
@dolanmiu dolanmiu added the bug label Jul 26, 2023
@Suniron
Copy link
Author

Suniron commented Jul 26, 2023

I'm using this fix but I'm not really proud of it 🥲

      const patchTheDocument = async (data: Buffer) => await patchDocument(data, {
        patches: {
          company_name: {
            children: [new TextRun('MY_COMPANY')],
            type: PatchType.PARAGRAPH,
          },
        },
      })

      // Patch the document a first time:
      let patchedDocxTemplate = await patchTheDocument(baseDocxTemplate)
      // Patch the document 10 times to avoid the case where multiple same keys are in the template (not patched), see https://github.com/dolanmiu/docx/issues/2267
      for (let i = 0; i < 10; i++) {
        patchedDocxTemplate = await patchTheDocument(Buffer.from(patchedDocxTemplate))
      }

dolanmiu added a commit that referenced this issue Dec 31, 2023
dolanmiu added a commit that referenced this issue Dec 31, 2023
* Turn patch document into options object

Add outputType to options

* Set keep styles to true by default

* Simplify method

* Rename variable

* #2267 Multiple patches of same key

* Remove path which won't be visited
@mateobv07
Copy link

I'm using this fix but I'm not really proud of it 🥲

      const patchTheDocument = async (data: Buffer) => await patchDocument(data, {
        patches: {
          company_name: {
            children: [new TextRun('MY_COMPANY')],
            type: PatchType.PARAGRAPH,
          },
        },
      })

      // Patch the document a first time:
      let patchedDocxTemplate = await patchTheDocument(baseDocxTemplate)
      // Patch the document 10 times to avoid the case where multiple same keys are in the template (not patched), see https://github.com/dolanmiu/docx/issues/2267
      for (let i = 0; i < 10; i++) {
        patchedDocxTemplate = await patchTheDocument(Buffer.from(patchedDocxTemplate))
      }

Im still having the same issue when the same word to be patched is multiple times in the same paragraph, using your fix worked but is there a better solution now?

@carlgleisner
Copy link

I'm also running into this 🤔

@Suniron
Copy link
Author

Suniron commented Feb 25, 2024

I don't really understand why my issue was closed.. 😞

@carlgleisner
Copy link

Ok @Suniron I'm going to attempt to debug this issue and provide a solution – if possible. Don't know when I'll be able to find the time to familiarise myself with the codebase but, yeah. It's the "open source thing" to do in this situation. Very happy to contribute! If I'm able to solve it.

To state the obvious: I guess it's a logical issue where subsequent occurrences of a {{patch}} within the same paragraph aren't processed.

If we put our minds together at some point, we really should be able to get to the bottom of this.

@mateobv07
Copy link

Thanks @carlgleisner If you need any help im down, I also really want this fixed :)

@mateobv07
Copy link

mateobv07 commented Feb 27, 2024

I started looking into this, I am pretty sure it has already been fixed lol. I think this is the problem #2605

It isnt released in the latest npm version. When I cloned the project and started running some tests it was actually working and replacing all patches in the same paragraph.

@carlgleisner
Copy link

carlgleisner commented Feb 28, 2024

I think this is the problem #2605

This makes a lot of sense 🙂

Amazing library – would have loved to contribute! I guess new opportunities will present themselves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants