TemplateContentExtractionContentMerger.ts
Merges extracted content into template content with attribute formatting and content separation.
This class handles the complex process of merging multiple extracted content pieces
into a single template string. It groups content by search and replace patterns,
formats attributes according to configured patterns, and applies content dividers
between multiple content blocks.
@example
const merger = new TemplateContentExtractionContentMerger({ target });
const templateContent = "# Content\n<h2 id="contentinjectionts">ContentInjection.ts</h2>
<p>Handles content injection into templates and files.</p>
<p>This class provides functionality to inject content into templates by replacing</p>
<p>placeholder strings. It supports both string-based injection and file-based</p>
<p>injection with template file reading and output file writing.</p>
<p>@example</p>
<div class="language-typescript highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="kd">const</span> <span class="nx">injection</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ContentInjection</span><span class="p">({</span>
<span class="na">template</span><span class="p">:</span> <span class="dl">'</span><span class="s1">./templates/doc.md</span><span class="dl">'</span><span class="p">,</span>
<span class="na">outDir</span><span class="p">:</span> <span class="dl">'</span><span class="s1">./docs</span><span class="dl">'</span><span class="p">,</span>
<span class="na">injectInto</span><span class="p">:</span> <span class="dl">'</span><span class="s1">%CONTENT%</span><span class="dl">'</span>
<span class="p">});</span>
<span class="c1">// Inject into string</span>
<span class="kd">const</span> <span class="nx">result</span> <span class="o">=</span> <span class="nx">injection</span><span class="p">.</span><span class="nx">injectIntoString</span><span class="p">(</span><span class="dl">'</span><span class="s1">Hello %CONTENT%</span><span class="dl">'</span><span class="p">,</span> <span class="dl">'</span><span class="s1">World</span><span class="dl">'</span><span class="p">);</span>
<span class="c1">// Returns 'Hello World'</span>
<span class="c1">// Inject into file</span>
<span class="nx">injection</span><span class="p">.</span><span class="nx">injectIntoFile</span><span class="p">(</span><span class="dl">'</span><span class="s1">Documentation content: %CONTENT%</span><span class="dl">'</span><span class="p">,</span> <span class="dl">'</span><span class="s1">output.md</span><span class="dl">'</span><span class="p">);</span>
</code></pre></div></div>
<hr />
<h2 id="methods">Methods</h2>
<h3 id="gettemplatecontentwithreplacestring"><strong>getTemplateContentWithReplaceString</strong></h3>
<p>Gets the template content with the replace string replaced.</p>
<p>@param replaceWith - The string to replace the replace string with</p>
<p>@param searchAndReplace - The search and replace string to replace</p>
<p>@returns The template content with the replace string replaced</p>
<hr />
<h3 id="mergeextractedcontentsintotemplatestring"><strong>mergeExtractedContentsIntoTemplateString</strong></h3>
<p>Creates a content string from extraction results by replacing the configured placeholder.</p>
<p>@param extractionResults - The extraction results to create the content from</p>
<p>@returns The content string with injected content</p>
<hr />
<h3 id="applydefaulttext"><strong>applyDefaultText</strong></h3>
<p>Applies the default text to the template content.</p>
<p>@param templateContent - The template content to apply the default text to</p>
<p>@returns The template content with the default text applied</p>
<hr />
<h3 id="writefile"><strong>writeFile</strong></h3>
<p>Injects content into a template file and writes the result to an output file.</p>
<p>@param replaceWith - The content to replace the placeholder with</p>
<p>@param outFile - The output file path to write the result to</p>
<p>@throws {Error} When the template file is not found</p>
<hr />
<p>Last updated: 2025-09-16T21:51:51.929Z</p>
<p>This file is auto generated. Do not edit manually.*</p>
<p><a href="/simple-docs-extractor/simple-docs-scraper/content/">Back to Index</a></p>
";
const extractedContentArray = [
{
content: "This is the content",
searchAndReplace: "<h2 id="contentinjectionts">ContentInjection.ts</h2>
<p>Handles content injection into templates and files.</p>
<p>This class provides functionality to inject content into templates by replacing</p>
<p>placeholder strings. It supports both string-based injection and file-based</p>
<p>injection with template file reading and output file writing.</p>
<p>@example</p>
<div class="language-typescript highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="kd">const</span> <span class="nx">injection</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ContentInjection</span><span class="p">({</span>
<span class="na">template</span><span class="p">:</span> <span class="dl">'</span><span class="s1">./templates/doc.md</span><span class="dl">'</span><span class="p">,</span>
<span class="na">outDir</span><span class="p">:</span> <span class="dl">'</span><span class="s1">./docs</span><span class="dl">'</span><span class="p">,</span>
<span class="na">injectInto</span><span class="p">:</span> <span class="dl">'</span><span class="s1">%CONTENT%</span><span class="dl">'</span>
<span class="p">});</span>
<span class="c1">// Inject into string</span>
<span class="kd">const</span> <span class="nx">result</span> <span class="o">=</span> <span class="nx">injection</span><span class="p">.</span><span class="nx">injectIntoString</span><span class="p">(</span><span class="dl">'</span><span class="s1">Hello %CONTENT%</span><span class="dl">'</span><span class="p">,</span> <span class="dl">'</span><span class="s1">World</span><span class="dl">'</span><span class="p">);</span>
<span class="c1">// Returns 'Hello World'</span>
<span class="c1">// Inject into file</span>
<span class="nx">injection</span><span class="p">.</span><span class="nx">injectIntoFile</span><span class="p">(</span><span class="dl">'</span><span class="s1">Documentation content: %CONTENT%</span><span class="dl">'</span><span class="p">,</span> <span class="dl">'</span><span class="s1">output.md</span><span class="dl">'</span><span class="p">);</span>
</code></pre></div></div>
<hr />
<h2 id="methods">Methods</h2>
<h3 id="gettemplatecontentwithreplacestring"><strong>getTemplateContentWithReplaceString</strong></h3>
<p>Gets the template content with the replace string replaced.</p>
<p>@param replaceWith - The string to replace the replace string with</p>
<p>@param searchAndReplace - The search and replace string to replace</p>
<p>@returns The template content with the replace string replaced</p>
<hr />
<h3 id="mergeextractedcontentsintotemplatestring"><strong>mergeExtractedContentsIntoTemplateString</strong></h3>
<p>Creates a content string from extraction results by replacing the configured placeholder.</p>
<p>@param extractionResults - The extraction results to create the content from</p>
<p>@returns The content string with injected content</p>
<hr />
<h3 id="applydefaulttext"><strong>applyDefaultText</strong></h3>
<p>Applies the default text to the template content.</p>
<p>@param templateContent - The template content to apply the default text to</p>
<p>@returns The template content with the default text applied</p>
<hr />
<h3 id="writefile"><strong>writeFile</strong></h3>
<p>Injects content into a template file and writes the result to an output file.</p>
<p>@param replaceWith - The content to replace the placeholder with</p>
<p>@param outFile - The output file path to write the result to</p>
<p>@throws {Error} When the template file is not found</p>
<hr />
<p>Last updated: 2025-09-16T21:51:51.929Z</p>
<p>This file is auto generated. Do not edit manually.*</p>
<p><a href="/simple-docs-extractor/simple-docs-scraper/content/">Back to Index</a></p>
",
attributes: { name: "John" }
}
];
const result = merger.handle(templateContent, extractedContentArray);
// Result: "# Content\n### *name*: John\n\nThis is the content"
@param {TemplateContentExtractionContentMergerConfig} config - Configuration containing the target for attribute format lookup
Methods
handle
Merges extracted content into template content by replacing placeholders.
Groups extracted content by search and replace patterns, then processes each
group to replace the corresponding placeholders in the template with formatted
content including attributes and dividers.
@param {string} templateContent - The template string containing placeholders
@param {ExtractedContent[]} extractedContentArray - Array of extracted content to merge
@returns {string} The template content with all placeholders replaced by formatted content
handleGrouped
Handles a grouped extraction result.
-
Iterate over the grouped extraction results and create a content block for each extraction result
-
Add the divide by to the content block
-
Replace the search and replace with the content block
-
Return the template content
handleExtractContent
Handles the extraction content.
-
Add the divide by to the content block
-
Add the content to the content block
-
Return the content block
buildAttributesContent
Builds the attributes content.
-
Add the attributes to the content block, using the attribute format defined in the extraction plugin
-
If no attribute format is defined, use the default format
-
Return the content block
getExtractionResultGroupedBySearchAndReplace
Gets the extraction results grouped by search and replace.
@param extractedContentArray - The extraction results to group by search and replace
@returns The extraction results grouped by search and replace
getDivideBy
Gets the divide by for an extraction result.
@param extractionResult - The extraction result to get the divide by for
@returns The divide by for the extraction result
Last updated: 2025-09-16T21:51:51.929Z
This file is auto generated. Do not edit manually.*