remark-captions
Advanced tools
Comparing version 2.2.3 to 2.2.4
"use strict"; | ||
var clone = require('clone'); | ||
var visit = require('unist-util-visit'); | ||
var xtend = require('xtend'); | ||
var legendBlock = { | ||
const clone = require('clone'); | ||
const visit = require('unist-util-visit'); | ||
const xtend = require('xtend'); | ||
const legendBlock = { | ||
table: 'Table:', | ||
code: 'Code:' | ||
}; | ||
var internLegendBlock = { | ||
const internLegendBlock = { | ||
blockquote: 'Source:', | ||
image: 'Figure:' | ||
}; | ||
function plugin(opts) { | ||
var externalBlocks = xtend(legendBlock, opts && opts.external || {}); | ||
var internalBlocks = xtend(internLegendBlock, opts && opts.internal || {}); | ||
var Compiler = this.Compiler; | ||
const externalBlocks = xtend(legendBlock, opts && opts.external || {}); | ||
const internalBlocks = xtend(internLegendBlock, opts && opts.internal || {}); | ||
const Compiler = this.Compiler; | ||
if (Compiler) { | ||
var visitors = Compiler.prototype.visitors; | ||
const visitors = Compiler.prototype.visitors; | ||
if (!visitors) return; | ||
visitors.figure = function (node) { | ||
var captionedNode = node.children[0]; | ||
var captionNode = node.children[1]; | ||
var captionedMarkdown = this.visit(captionedNode); // compile without taking care of the "figcaption" wrapper node | ||
const captionedNode = node.children[0]; | ||
const captionNode = node.children[1]; | ||
const captionedMarkdown = this.visit(captionedNode); | ||
var captionMarkdown = this.all(captionNode).join(''); | ||
// compile without taking care of the "figcaption" wrapper node | ||
const captionMarkdown = this.all(captionNode).join(''); | ||
if (!(captionedNode.type in externalBlocks || captionedNode.type in internalBlocks)) { | ||
return captionedMarkdown; | ||
} | ||
var prefix = ''; | ||
let prefix = ''; | ||
if (captionedNode.type in externalBlocks) { | ||
@@ -45,15 +37,9 @@ prefix = externalBlocks[captionedNode.type]; | ||
} | ||
return "".concat(captionedMarkdown, "\n").concat(prefix, " ").concat(captionMarkdown); | ||
return `${captionedMarkdown}\n${prefix} ${captionMarkdown}`; | ||
}; | ||
} | ||
return function transformer(tree) { | ||
Object.keys(internalBlocks).forEach(function (nodeType) { | ||
return visit(tree, nodeType, internLegendVisitor(internalBlocks)); | ||
}); | ||
Object.keys(externalBlocks).forEach(function (nodeType) { | ||
return visit(tree, nodeType, externLegendVisitorCreator(externalBlocks)); | ||
}); | ||
visit(tree, 'figure', function (figure, index, parent) { | ||
Object.keys(internalBlocks).forEach(nodeType => visit(tree, nodeType, internLegendVisitor(internalBlocks))); | ||
Object.keys(externalBlocks).forEach(nodeType => visit(tree, nodeType, externLegendVisitorCreator(externalBlocks))); | ||
visit(tree, 'figure', (figure, index, parent) => { | ||
if (parent.type === 'paragraph') { | ||
@@ -66,13 +52,9 @@ if (index === 0) { | ||
} | ||
parent.type = 'tempWrapper'; | ||
} | ||
}); | ||
visit(tree, 'tempWrapper', function (wrapper, index, parent) { | ||
var _parent$children; | ||
var newChildren = []; | ||
wrapper.children.forEach(function (node, i) { | ||
var child = clone(node); | ||
visit(tree, 'tempWrapper', (wrapper, index, parent) => { | ||
const newChildren = []; | ||
wrapper.children.forEach((node, i) => { | ||
const child = clone(node); | ||
if (child.type === 'figure') { | ||
@@ -82,3 +64,2 @@ newChildren.push(child); | ||
} | ||
if (child.type === 'text' && !child.value.trim()) { | ||
@@ -89,3 +70,2 @@ return; | ||
} | ||
wrapper.children[i].type = 'paragraph'; | ||
@@ -95,56 +75,57 @@ wrapper.children[i].children = [child]; | ||
}); | ||
(_parent$children = parent.children).splice.apply(_parent$children, [index, 1].concat(newChildren)); | ||
parent.children.splice(index, 1, ...newChildren); | ||
}); | ||
}; | ||
} | ||
function internLegendVisitor(internalBlocks) { | ||
return function (node, index, parent) { | ||
// if already wrapped in figure, skip | ||
if (parent && parent.type === 'figure') return; // if the current node has some children, the legend is the last child. | ||
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. | ||
var lastP = node.children ? getLastParagraph(node.children) : parent; // legend can only be in a paragraph. | ||
const 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 | ||
} | ||
var legendChildIndex = -1; | ||
lastP.children.forEach(function (child, index) { | ||
if (child.type === 'text' && (child.value.startsWith(internalBlocks[node.type]) || child.value.includes("\n".concat(internalBlocks[node.type])))) { | ||
// find which child contains the last legend | ||
let legendChildIndex = -1; | ||
lastP.children.forEach((child, index) => { | ||
if (child.type === 'text' && (child.value.startsWith(internalBlocks[node.type]) || child.value.includes(`\n${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 | ||
} | ||
var potentialLegendLines = lastP.children[legendChildIndex].value.split('\n'); | ||
var lastLegendIndex = -1; | ||
potentialLegendLines.forEach(function (line, index) { | ||
// split the text node containing the last legend and find the line containing it | ||
const potentialLegendLines = lastP.children[legendChildIndex].value.split('\n'); | ||
let lastLegendIndex = -1; | ||
potentialLegendLines.forEach((line, index) => { | ||
if (line.startsWith(internalBlocks[node.type])) { | ||
lastLegendIndex = index; | ||
} | ||
}); // the child containing the last legend is split in two: head contains text until | ||
}); | ||
// the child containing the last legend is split in two: head contains text until | ||
// legend, tail contains legend text | ||
const tail = clone(lastP.children[legendChildIndex]); | ||
const headText = potentialLegendLines.slice(0, lastLegendIndex).join('\n'); | ||
// replace existing node 'head' content with text until legend | ||
lastP.children[legendChildIndex].value = headText; | ||
var tail = clone(lastP.children[legendChildIndex]); | ||
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… | ||
const legendText = potentialLegendLines.slice(lastLegendIndex).join('\n').slice(internalBlocks[node.type].length).trimLeft(); | ||
tail.value = legendText; | ||
// … and 'tail', the cloned node is inserted after 'head' | ||
lastP.children.splice(legendChildIndex + 1, 0, tail); | ||
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' | ||
lastP.children.splice(legendChildIndex + 1, 0, tail); // gather all nodes that should be inside the legend | ||
var legendNodes = lastP.children.slice(legendChildIndex + 1); // remove them from the parent paragraph | ||
// gather all nodes that should be inside the legend | ||
const legendNodes = lastP.children.slice(legendChildIndex + 1); | ||
// remove them from the parent paragraph | ||
lastP.children = lastP.children.slice(0, legendChildIndex + 1); | ||
var figcaption = { | ||
const figcaption = { | ||
type: 'figcaption', | ||
@@ -156,3 +137,3 @@ children: legendNodes, | ||
}; | ||
var figure = { | ||
const figure = { | ||
type: 'figure', | ||
@@ -169,3 +150,2 @@ children: [clone(node), figcaption], | ||
} | ||
function externLegendVisitorCreator(blocks) { | ||
@@ -175,9 +155,8 @@ return function (node, index, parent) { | ||
if (parent.children[index + 1].type !== 'paragraph') return; | ||
var legendNode = parent.children[index + 1]; | ||
var firstChild = legendNode.children[0]; | ||
const legendNode = parent.children[index + 1]; | ||
const firstChild = legendNode.children[0]; | ||
if (firstChild.type !== 'text' || !firstChild.value.startsWith(blocks[node.type])) return; | ||
var legendNodes = []; | ||
var followingNodes = []; | ||
var firstTextLine = firstChild.value.replace(blocks[node.type], '').split('\n')[0]; | ||
const legendNodes = []; | ||
const followingNodes = []; | ||
const firstTextLine = firstChild.value.replace(blocks[node.type], '').split('\n')[0]; | ||
if (firstChild.value.includes('\n')) { | ||
@@ -189,14 +168,10 @@ followingNodes.push({ | ||
} | ||
legendNodes.push({ | ||
type: 'text', | ||
value: firstTextLine.trimLeft() // remove the " " after the {prefix}: | ||
}); | ||
legendNode.children.forEach(function (node, index) { | ||
legendNode.children.forEach((node, index) => { | ||
if (index === 0) return; | ||
if (node.type === 'text') { | ||
var keepInLegend = node.value.split('\n')[0]; | ||
const keepInLegend = node.value.split('\n')[0]; | ||
if (node.value.includes('\n')) { | ||
@@ -206,3 +181,2 @@ node.value = node.value.split('\n')[1]; | ||
} | ||
legendNodes.push({ | ||
@@ -216,3 +190,3 @@ type: 'text', | ||
}); | ||
var figcaption = { | ||
const figcaption = { | ||
type: 'figcaption', | ||
@@ -224,3 +198,3 @@ children: legendNodes, | ||
}; | ||
var figure = { | ||
const figure = { | ||
type: 'figure', | ||
@@ -235,3 +209,2 @@ children: [clone(node), figcaption], | ||
node.data = figure.data; | ||
if (followingNodes.length) { | ||
@@ -247,7 +220,6 @@ parent.children.splice(index + 1, 1, { | ||
} | ||
function getLastParagraph(xs, lastParagraph) { | ||
var len = xs.length; | ||
const len = xs.length; | ||
if (!len) return; | ||
var last = xs[len - 1]; | ||
const last = xs[len - 1]; | ||
if (last.type === 'text') return lastParagraph; | ||
@@ -258,3 +230,2 @@ if (!last.children || !last.children.length) return lastParagraph; | ||
} | ||
module.exports = plugin; |
{ | ||
"name": "remark-captions", | ||
"version": "2.2.3", | ||
"version": "2.2.4", | ||
"repository": { | ||
@@ -16,3 +16,3 @@ "url": "https://github.com/zestedesavoir/zmarkdown/tree/master/packages/remark-captions", | ||
"pretest": "eslint .", | ||
"build": "del-cli dist && cross-env BABEL_ENV=production babel src --out-dir dist", | ||
"build": "babel --root-mode upward --delete-dir-on-start --env-name production --out-dir dist src", | ||
"test": "jest", | ||
@@ -37,3 +37,3 @@ "coverage": "jest --coverage" | ||
}, | ||
"gitHead": "5a6a1dde9a9ea91c0e7d64b4b32dda4927607bbf" | ||
"gitHead": "b278abe5320c5ca01bdaed3c833f5f68ab389663" | ||
} |
@@ -7,3 +7,3 @@ const clone = require('clone') | ||
table: 'Table:', | ||
code: 'Code:', | ||
code: 'Code:' | ||
} | ||
@@ -13,3 +13,3 @@ | ||
blockquote: 'Source:', | ||
image: 'Figure:', | ||
image: 'Figure:' | ||
} | ||
@@ -154,4 +154,4 @@ | ||
data: { | ||
hName: 'figcaption', | ||
}, | ||
hName: 'figcaption' | ||
} | ||
} | ||
@@ -163,7 +163,7 @@ | ||
clone(node), | ||
figcaption, | ||
figcaption | ||
], | ||
data: { | ||
hName: 'figure', | ||
}, | ||
hName: 'figure' | ||
} | ||
} | ||
@@ -191,8 +191,10 @@ | ||
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}: | ||
value: firstTextLine.trimLeft() // remove the " " after the {prefix}: | ||
}) | ||
@@ -209,3 +211,3 @@ | ||
} | ||
legendNodes.push({type: 'text', value: keepInLegend}) | ||
legendNodes.push({ type: 'text', value: keepInLegend }) | ||
} else { | ||
@@ -220,4 +222,4 @@ legendNodes.push(clone(node)) | ||
data: { | ||
hName: 'figcaption', | ||
}, | ||
hName: 'figcaption' | ||
} | ||
} | ||
@@ -228,7 +230,7 @@ const figure = { | ||
clone(node), | ||
figcaption, | ||
figcaption | ||
], | ||
data: { | ||
hName: 'figure', | ||
}, | ||
hName: 'figure' | ||
} | ||
} | ||
@@ -241,3 +243,3 @@ | ||
if (followingNodes.length) { | ||
parent.children.splice(index + 1, 1, {type: 'paragraph', children: followingNodes}) | ||
parent.children.splice(index + 1, 1, { type: 'paragraph', children: followingNodes }) | ||
} else { | ||
@@ -244,0 +246,0 @@ parent.children.splice(index + 1, 1) |
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
427
21654