// replace text variables with WordFragments using default block type replacements in a DOCX template

import { CreateDocx, OptionsText, 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 => {
    // create a text WordFragment
    const wordFragmentText = new WordFragment();
    const text: OptionsText = {
        bold: true,
        italic: true,
        fontSize: 14,
        text: "New content.",
    };
    wordFragmentText.addText([text]);

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

    // replace variables in the document target
    docx.replaceVariableWordFragment(variablesDocument);

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