easy-template-x
Advanced tools
Comparing version 3.0.4 to 3.1.0
@@ -11,2 +11,4 @@ import { DocxParser } from '../office'; | ||
findDelimiters(node: XmlNode): DelimiterMark[]; | ||
private noMatch; | ||
private fullMatch; | ||
private shouldSearchNode; | ||
@@ -13,0 +15,0 @@ private findNextNode; |
@@ -8,2 +8,3 @@ import { MimeType } from '../../mimeType'; | ||
source: Binary; | ||
altText?: string; | ||
format: ImageFormat; | ||
@@ -10,0 +11,0 @@ width: number; |
@@ -7,4 +7,5 @@ import { ScopeData, Tag, TemplateContext } from '../../compilation'; | ||
private createMarkup; | ||
private docProperties; | ||
private pictureMarkup; | ||
private pixelsToEmu; | ||
} |
{ | ||
"name": "easy-template-x", | ||
"version": "3.0.4", | ||
"version": "3.1.0", | ||
"description": "Generate docx documents from templates, in Node or in the browser.", | ||
@@ -42,3 +42,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@xmldom/xmldom": "0.8.6", | ||
"@xmldom/xmldom": "0.8.7", | ||
"jszip": "3.10.1", | ||
@@ -45,0 +45,0 @@ "lodash.get": "4.4.2" |
@@ -272,2 +272,3 @@ # easy-template-x | ||
format: MimeType.Png, | ||
altText: "Kung Fu Hero", // Optional | ||
width: 200, | ||
@@ -298,3 +299,3 @@ height: 200 | ||
_type: 'link', | ||
text: 'super easy', // optional - if not specified the `target` property will be used | ||
text: 'super easy', // Optional - if not specified the `target` property will be used | ||
target: 'https://github.com/alonrbar/easy-template-x' | ||
@@ -328,3 +329,3 @@ } | ||
xml: '<w:sym w:font="Wingdings" w:char="F04A"/>', | ||
replaceParagraph: false, // optional - should the plugin replace an entire paragraph or just the tag itself | ||
replaceParagraph: false, // Optional - should the plugin replace an entire paragraph or just the tag itself | ||
} | ||
@@ -331,0 +332,0 @@ } |
@@ -34,3 +34,3 @@ import { MissingArgumentError } from '../errors'; | ||
// | ||
// Performance note: | ||
// Performance note: | ||
// | ||
@@ -69,65 +69,25 @@ // The search efficiency is o(m*n) where n is the text size and m is the | ||
const delimiterPattern = lookForOpenDelimiter ? this.startDelimiter : this.endDelimiter; | ||
// char match | ||
const char = node.textContent[textIndex]; | ||
if (char === delimiterPattern[match.delimiterIndex]) { | ||
// first match | ||
if (match.firstMatchIndex === -1) { | ||
match.firstMatchIndex = textIndex; | ||
} | ||
// no match | ||
if (char !== delimiterPattern[match.delimiterIndex]) { | ||
[node, textIndex] = this.noMatch(node, textIndex, match); | ||
textIndex++; | ||
continue; | ||
} | ||
// full delimiter match | ||
if (match.delimiterIndex === delimiterPattern.length - 1) { | ||
// move all delimiters characters to the same text node | ||
if (match.openNodes.length > 1) { | ||
const firstNode = first(match.openNodes); | ||
const lastNode = last(match.openNodes); | ||
this.docxParser.joinTextNodesRange(firstNode, lastNode); | ||
textIndex += (firstNode.textContent.length - node.textContent.length); | ||
node = firstNode; | ||
} | ||
// store delimiter | ||
const delimiterMark = this.createDelimiterMark(match, lookForOpenDelimiter); | ||
delimiters.push(delimiterMark); | ||
// update state | ||
lookForOpenDelimiter = !lookForOpenDelimiter; | ||
match.reset(); | ||
if (textIndex < node.textContent.length - 1) { | ||
match.openNodes.push(node); | ||
} | ||
} else { | ||
match.delimiterIndex++; | ||
} | ||
// first match | ||
if (match.firstMatchIndex === -1) { | ||
match.firstMatchIndex = textIndex; | ||
} | ||
// no match | ||
else { | ||
// | ||
// go back to first open node | ||
// | ||
// Required for cases where the text has repeating | ||
// characters that are the same as a delimiter prefix. | ||
// For instance: | ||
// Delimiter is '{!' and template text contains the string '{{!' | ||
// | ||
if (match.firstMatchIndex !== -1) { | ||
node = first(match.openNodes); | ||
textIndex = match.firstMatchIndex; | ||
} | ||
// update state | ||
match.reset(); | ||
if (textIndex < node.textContent.length - 1) { | ||
match.openNodes.push(node); | ||
} | ||
// partial match | ||
if (match.delimiterIndex !== delimiterPattern.length - 1) { | ||
match.delimiterIndex++; | ||
textIndex++; | ||
continue; | ||
} | ||
// full delimiter match | ||
[node, textIndex, lookForOpenDelimiter] = this.fullMatch(node, textIndex, lookForOpenDelimiter, match, delimiters); | ||
textIndex++; | ||
@@ -142,2 +102,52 @@ } | ||
private noMatch(node: XmlTextNode, textIndex: number, match: MatchState): [XmlTextNode, number] { | ||
// | ||
// go back to first open node | ||
// | ||
// Required for cases where the text has repeating | ||
// characters that are the same as a delimiter prefix. | ||
// For instance: | ||
// Delimiter is '{!' and template text contains the string '{{!' | ||
// | ||
if (match.firstMatchIndex !== -1) { | ||
node = first(match.openNodes); | ||
textIndex = match.firstMatchIndex; | ||
} | ||
// update state | ||
match.reset(); | ||
if (textIndex < node.textContent.length - 1) { | ||
match.openNodes.push(node); | ||
} | ||
return [node, textIndex]; | ||
} | ||
private fullMatch(node: XmlTextNode, textIndex: number, lookForOpenDelimiter: boolean, match: MatchState, delimiters: DelimiterMark[]): [XmlTextNode, number, boolean] { | ||
// move all delimiters characters to the same text node | ||
if (match.openNodes.length > 1) { | ||
const firstNode = first(match.openNodes); | ||
const lastNode = last(match.openNodes); | ||
this.docxParser.joinTextNodesRange(firstNode, lastNode); | ||
textIndex += (firstNode.textContent.length - node.textContent.length); | ||
node = firstNode; | ||
} | ||
// store delimiter | ||
const delimiterMark = this.createDelimiterMark(match, lookForOpenDelimiter); | ||
delimiters.push(delimiterMark); | ||
// update state | ||
lookForOpenDelimiter = !lookForOpenDelimiter; | ||
match.reset(); | ||
if (textIndex < node.textContent.length - 1) { | ||
match.openNodes.push(node); | ||
} | ||
return [node, textIndex, lookForOpenDelimiter]; | ||
} | ||
private shouldSearchNode(node: XmlNode): node is XmlTextNode { | ||
@@ -192,2 +202,2 @@ | ||
} | ||
} | ||
} |
@@ -10,2 +10,6 @@ import { MimeType } from '../../mimeType'; | ||
source: Binary; | ||
/** | ||
* Optional. If this is not set the image will be marked as "decorative". | ||
*/ | ||
altText?: string; | ||
format: ImageFormat; | ||
@@ -12,0 +16,0 @@ width: number; |
@@ -41,3 +41,3 @@ import { ScopeData, Tag, TemplateContext } from '../../compilation'; | ||
const imageId = nextImageId++; | ||
const imageXml = this.createMarkup(imageId, relId, content.width, content.height); | ||
const imageXml = this.createMarkup(imageId, relId, content.altText, content.width, content.height); | ||
@@ -48,3 +48,3 @@ XmlNode.insertAfter(imageXml, wordTextNode); | ||
private createMarkup(imageId: number, relId: string, width: number, height: number): XmlNode { | ||
private createMarkup(imageId: number, relId: string, altText: string, width: number, height: number): XmlNode { | ||
@@ -69,3 +69,3 @@ // http://officeopenxml.com/drwPicInline.php | ||
<wp:effectExtent l="0" t="0" r="0" b="0"/> | ||
<wp:docPr id="${imageId}" name="${name}"/> | ||
${this.docProperties(imageId, name, altText)} | ||
<wp:cNvGraphicFramePr> | ||
@@ -76,3 +76,3 @@ <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/> | ||
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"> | ||
${this.pictureMarkup(name, relId, width, height)} | ||
${this.pictureMarkup(imageId, relId, name, width, height)} | ||
</a:graphicData> | ||
@@ -90,4 +90,20 @@ </a:graphic> | ||
private pictureMarkup(name: string, relId: string, width: number, height: number) { | ||
private docProperties(imageId: number, name: string, altText: string): string { | ||
if (altText) { | ||
return `<wp:docPr id="${imageId}" name="${name}" descr="${altText}"/>`; | ||
} | ||
return ` | ||
<wp:docPr id="${imageId}" name="${name}"> | ||
<a:extLst xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"> | ||
<a:ext uri="{C183D7F6-B498-43B3-948B-1728B52AA6E4}"> | ||
<adec:decorative xmlns:adec="http://schemas.microsoft.com/office/drawing/2017/decorative" val="1"/> | ||
</a:ext> | ||
</a:extLst> | ||
</wp:docPr> | ||
`; | ||
} | ||
private pictureMarkup(imageId: number, relId: string, name: string, width: number, height: number) { | ||
// http://officeopenxml.com/drwPic.php | ||
@@ -103,3 +119,3 @@ | ||
<pic:nvPicPr> | ||
<pic:cNvPr id="0" name="${name}"/> | ||
<pic:cNvPr id="${imageId}" name="${name}"/> | ||
<pic:cNvPicPr> | ||
@@ -106,0 +122,0 @@ <a:picLocks noChangeAspect="1" noChangeArrowheads="1"/> |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
391443
9775
572
+ Added@xmldom/xmldom@0.8.7(transitive)
- Removed@xmldom/xmldom@0.8.6(transitive)
Updated@xmldom/xmldom@0.8.7