// replace a text variable with WordML in a DOCX template

import { CreateDocx, WordFragment } from "../../../build";
import * as fs from "fs";

const docxTemplate = fs.readFileSync("../../files/TemplateVariables.docx");

const docx = new CreateDocx();
// load the template
docx.openTemplate(docxTemplate).then(docx => {
    const wordMlFragment = new WordFragment();
    wordMlFragment.addWordMl(
        '<w:p><w:r><w:t xml:space="preserve">This is a simple paragraph with some </w:t></w:r><w:r><w:rPr><w:b/></w:rPr><w:t>bold</w:t></w:r><w:r><w:t xml:space="preserve"> text.</w:t></w:r></w:p>'
    );

    // variables to be replaced
    const variablesDocument = new Map<string, WordFragment>();
    variablesDocument.set("VARNAME", wordMlFragment);

    docx.replaceVariableWordMl(variablesDocument);

    // save the DOCX
    docx.save().then(contents => {
        fs.writeFileSync("replaceVariableWordMl_1.docx", Buffer.from(contents));
    });
});
