remark-captions
Advanced tools
Comparing version 2.1.6 to 2.1.7
@@ -1,5 +0,7 @@ | ||
'use strict'; | ||
"use strict"; | ||
var clone = require('clone'); | ||
var visit = require('unist-util-visit'); | ||
var xtend = require('xtend'); | ||
@@ -11,3 +13,2 @@ | ||
}; | ||
var internLegendBlock = { | ||
@@ -21,4 +22,4 @@ blockquote: 'Source:', | ||
var internalBlocks = xtend(internLegendBlock, opts && opts.internal || {}); | ||
var Compiler = this.Compiler; | ||
var Compiler = this.Compiler; | ||
if (Compiler) { | ||
@@ -31,6 +32,6 @@ var visitors = Compiler.prototype.visitors; | ||
var captionNode = node.children[1]; | ||
var captionedMarkdown = this.visit(captionedNode); | ||
var captionedMarkdown = this.visit(captionedNode); // compile without taking care of the "figcaption" wrapper node | ||
// compile without taking care of the "figcaption" wrapper node | ||
var captionMarkdown = this.all(captionNode).join(''); | ||
if (!(captionedNode.type in externalBlocks || captionedNode.type in internalBlocks)) { | ||
@@ -41,2 +42,3 @@ return captionedMarkdown; | ||
var prefix = ''; | ||
if (captionedNode.type in externalBlocks) { | ||
@@ -48,3 +50,3 @@ prefix = externalBlocks[captionedNode.type]; | ||
return captionedMarkdown + '\n' + prefix + ' ' + captionMarkdown; | ||
return "".concat(captionedMarkdown, "\n").concat(prefix, " ").concat(captionMarkdown); | ||
}; | ||
@@ -57,3 +59,2 @@ } | ||
}); | ||
Object.keys(externalBlocks).forEach(function (nodeType) { | ||
@@ -70,2 +71,3 @@ return visit(tree, nodeType, externLegendVisitorCreator(externalBlocks)); | ||
} | ||
parent.type = 'tempWrapper'; | ||
@@ -80,2 +82,3 @@ } | ||
var child = clone(node); | ||
if (child.type === 'figure') { | ||
@@ -85,2 +88,3 @@ newChildren.push(child); | ||
} | ||
if (child.type === 'text' && !child.value.trim()) { | ||
@@ -91,2 +95,3 @@ return; | ||
} | ||
wrapper.children[i].type = 'paragraph'; | ||
@@ -96,2 +101,3 @@ wrapper.children[i].children = [child]; | ||
}); | ||
(_parent$children = parent.children).splice.apply(_parent$children, [index, 1].concat(newChildren)); | ||
@@ -105,24 +111,24 @@ }); | ||
// if already wrapped in figure, skip | ||
if (parent && parent.type === 'figure') return; | ||
if (parent && parent.type === 'figure') return; // if the current node has some children, the legend is the last child. | ||
// if not, the legend is the last child of the parent node. | ||
// if the current node has some children, the legend is the last child. | ||
// if not, the legend is the last child of the parent node. | ||
var lastP = node.children ? getLastParagraph(node.children) : parent; | ||
// legend can only be in a paragraph. | ||
var lastP = node.children ? getLastParagraph(node.children) : parent; // legend can only be in a paragraph. | ||
if (!lastP || node.children && lastP.type !== 'paragraph' || !node.children && parent.type !== 'paragraph') { | ||
return; | ||
} | ||
} // find which child contains the last legend | ||
// find which child contains the last legend | ||
var legendChildIndex = -1; | ||
lastP.children.forEach(function (child, index) { | ||
if (child.type === 'text' && (child.value.startsWith(internalBlocks[node.type]) || child.value.includes('\n' + internalBlocks[node.type]))) { | ||
if (child.type === 'text' && (child.value.startsWith(internalBlocks[node.type]) || child.value.includes("\n".concat(internalBlocks[node.type])))) { | ||
legendChildIndex = index; | ||
} | ||
}); | ||
if (legendChildIndex === -1 || !node.children && legendChildIndex < index) { | ||
return; | ||
} | ||
} // split the text node containing the last legend and find the line containing it | ||
// split the text node containing the last legend and find the line containing it | ||
var potentialLegendLines = lastP.children[legendChildIndex].value.split('\n'); | ||
@@ -134,23 +140,18 @@ var lastLegendIndex = -1; | ||
} | ||
}); | ||
}); // the child containing the last legend is split in two: head contains text until | ||
// legend, tail contains legend text | ||
// the child containing the last legend is split in two: head contains text until | ||
// legend, tail contains legend text | ||
var tail = clone(lastP.children[legendChildIndex]); | ||
var headText = potentialLegendLines.slice(0, lastLegendIndex).join('\n'); | ||
// replace existing node 'head' content with text until legend | ||
lastP.children[legendChildIndex].value = headText; | ||
var headText = potentialLegendLines.slice(0, lastLegendIndex).join('\n'); // replace existing node 'head' content with text until legend | ||
// legend text is put into the cloned node… | ||
lastP.children[legendChildIndex].value = headText; // legend text is put into the cloned node… | ||
var legendText = potentialLegendLines.slice(lastLegendIndex).join('\n').slice(internalBlocks[node.type].length).trimLeft(); | ||
tail.value = legendText; // … and 'tail', the cloned node is inserted after 'head' | ||
tail.value = legendText; | ||
// … and 'tail', the cloned node is inserted after 'head' | ||
lastP.children.splice(legendChildIndex + 1, 0, tail); | ||
lastP.children.splice(legendChildIndex + 1, 0, tail); // gather all nodes that should be inside the legend | ||
// gather all nodes that should be inside the legend | ||
var legendNodes = lastP.children.slice(legendChildIndex + 1); | ||
// remove them from the parent paragraph | ||
var legendNodes = lastP.children.slice(legendChildIndex + 1); // remove them from the parent paragraph | ||
lastP.children = lastP.children.slice(0, legendChildIndex + 1); | ||
var figcaption = { | ||
@@ -163,3 +164,2 @@ type: 'figcaption', | ||
}; | ||
var figure = { | ||
@@ -172,3 +172,2 @@ type: 'figure', | ||
}; | ||
node.type = figure.type; | ||
@@ -184,7 +183,5 @@ node.children = figure.children; | ||
if (parent.children[index + 1].type !== 'paragraph') return; | ||
var legendNode = parent.children[index + 1]; | ||
var firstChild = legendNode.children[0]; | ||
if (firstChild.type !== 'text' || !firstChild.value.startsWith(blocks[node.type])) return; | ||
var legendNodes = []; | ||
@@ -195,10 +192,13 @@ var followingNodes = []; | ||
if (firstChild.value.includes('\n')) { | ||
followingNodes.push({ type: 'text', | ||
value: firstChild.value.replace(blocks[node.type], '').split('\n')[1] }); | ||
followingNodes.push({ | ||
type: 'text', | ||
value: firstChild.value.replace(blocks[node.type], '').split('\n')[1] | ||
}); | ||
} | ||
legendNodes.push({ | ||
type: 'text', | ||
value: firstTextLine.trimLeft() // remove the " " after the {prefix}: | ||
}); | ||
legendNode.children.forEach(function (node, index) { | ||
@@ -209,2 +209,3 @@ if (index === 0) return; | ||
var keepInLegend = node.value.split('\n')[0]; | ||
if (node.value.includes('\n')) { | ||
@@ -214,3 +215,7 @@ node.value = node.value.split('\n')[1]; | ||
} | ||
legendNodes.push({ type: 'text', value: keepInLegend }); | ||
legendNodes.push({ | ||
type: 'text', | ||
value: keepInLegend | ||
}); | ||
} else { | ||
@@ -220,3 +225,2 @@ legendNodes.push(clone(node)); | ||
}); | ||
var figcaption = { | ||
@@ -236,3 +240,2 @@ type: 'figcaption', | ||
}; | ||
node.type = figure.type; | ||
@@ -243,3 +246,6 @@ node.children = figure.children; | ||
if (followingNodes.length) { | ||
parent.children.splice(index + 1, 1, { type: 'paragraph', children: followingNodes }); | ||
parent.children.splice(index + 1, 1, { | ||
type: 'paragraph', | ||
children: followingNodes | ||
}); | ||
} else { | ||
@@ -254,7 +260,5 @@ parent.children.splice(index + 1, 1); | ||
if (!len) return; | ||
var last = xs[len - 1]; | ||
if (last.type === 'text') return lastParagraph; | ||
if (!last.children || !last.children.length) return lastParagraph; | ||
if (last.type === 'paragraph') return getLastParagraph(last.children, last); | ||
@@ -261,0 +265,0 @@ return getLastParagraph(last.children, lastParagraph); |
{ | ||
"name": "remark-captions", | ||
"version": "2.1.6", | ||
"version": "2.1.7", | ||
"repository": { | ||
@@ -38,3 +38,3 @@ "url": "https://github.com/zestedesavoir/zmarkdown/tree/master/packages/remark-captions", | ||
}, | ||
"gitHead": "1dded309a2670689a4a3353f9e38b80624c6df1a" | ||
"gitHead": "7b61723ac4ca1900270d56c7787c08b6887fcb4c" | ||
} |
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
21775
419