remark-captions
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -12,51 +12,89 @@ 'use strict'; | ||
var internLegendBlock = { | ||
blockquote: 'Source:', | ||
img: 'Figure:' | ||
}; | ||
function plugin(opts) { | ||
var blocks = xtend(legendBlock, opts || {}); | ||
return transformer; | ||
var externalBlocks = xtend(legendBlock, opts && opts.external || {}); | ||
var internalBlocks = xtend(internLegendBlock, opts && opts.internal || {}); | ||
function transformer(tree) { | ||
visit(tree, 'blockquote', internLegendVisitor); | ||
return function transformer(tree) { | ||
Object.keys(internalBlocks).forEach(function (nodeType) { | ||
return visit(tree, nodeType, internLegendVisitor(internalBlocks)); | ||
}); | ||
Object.keys(legendBlock).forEach(function (nodeType) { | ||
return visit(tree, nodeType, externLegendVisitorCreator(blocks)); | ||
Object.keys(externalBlocks).forEach(function (nodeType) { | ||
return visit(tree, nodeType, externLegendVisitorCreator(externalBlocks)); | ||
}); | ||
} | ||
}; | ||
} | ||
function internLegendVisitor(node, index, parent) { | ||
if (parent && parent.type === 'figure') return; | ||
var lastP = getLast(node.children); | ||
if (!lastP || lastP.type !== 'paragraph') return; | ||
var lastT = getLast(lastP.children); | ||
if (!lastT || lastT.type !== 'text') return; | ||
var lines = lastT.value.split('\n'); | ||
var lastLine = getLast(lines); | ||
if (!lastLine) return; | ||
if (!lastLine.startsWith('Source:')) return; | ||
var legend = lines.pop().slice(lastLine.indexOf(':') + 1).trim(); | ||
function internLegendVisitor(internalBlocks) { | ||
return function (node, index, parent) { | ||
lastT.value = lines.join('\n'); | ||
// if already wrapped in figure, skip | ||
if (parent && parent.type === 'figure') return; | ||
var figcaption = { | ||
type: 'figcaption', | ||
children: [{ | ||
type: 'text', | ||
value: legend | ||
}], | ||
data: { | ||
hName: 'figcaption' | ||
// legend can only be in a paragraph | ||
var lastP = getLast(node.children); | ||
if (!lastP || lastP.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' + internalBlocks[node.type]))) { | ||
legendChildIndex = index; | ||
} | ||
}); | ||
if (legendChildIndex === -1) 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) { | ||
if (line.startsWith(internalBlocks[node.type])) { | ||
lastLegendIndex = index; | ||
} | ||
} | ||
}; | ||
var figure = { | ||
type: 'figure', | ||
children: [clone(node), figcaption], | ||
data: { | ||
hName: 'figure' | ||
} | ||
// 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; | ||
// 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 | ||
);lastP.children = lastP.children.slice(0, legendChildIndex + 1); | ||
var figcaption = { | ||
type: 'figcaption', | ||
children: legendNodes, | ||
data: { | ||
hName: 'figcaption' | ||
} | ||
}; | ||
var figure = { | ||
type: 'figure', | ||
children: [clone(node), figcaption], | ||
data: { | ||
hName: 'figure' | ||
} | ||
}; | ||
node.type = figure.type; | ||
node.children = figure.children; | ||
node.data = figure.data; | ||
}; | ||
node.type = figure.type; | ||
node.children = figure.children; | ||
node.data = figure.data; | ||
} | ||
@@ -71,14 +109,31 @@ | ||
if (firstChild.value.startsWith(blocks[node.type])) { | ||
var firstLine = firstChild.value.split('\n')[0]; | ||
var legendText = firstLine.replace(blocks[node.type], '').trim(); | ||
var fullLegendLine = blocks[node.type] + ' ' + legendText; | ||
var legendNodes = []; | ||
var followingNodes = []; | ||
var firstTextLine = firstChild.value.replace(blocks[node.type], '').split('\n')[0]; | ||
if (firstChild.value.includes('\n')) { | ||
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}: | ||
() }); | ||
firstChild.value = firstChild.value.replace(fullLegendLine, '').trim(); | ||
legendNode.children.forEach(function (node, index) { | ||
if (index === 0) return; | ||
if (node.type === 'text') { | ||
var keepInLegend = node.value.split('\n')[0]; | ||
if (node.value.includes('\n')) { | ||
node.value = node.value.split('\n')[1]; | ||
followingNodes.push(node); | ||
} | ||
legendNodes.push({ type: 'text', value: keepInLegend }); | ||
} else { | ||
legendNodes.push(clone(node)); | ||
} | ||
}); | ||
var figcaption = { | ||
type: 'figcaption', | ||
children: [{ | ||
type: 'text', | ||
value: legendText | ||
}], | ||
children: legendNodes, | ||
data: { | ||
@@ -98,3 +153,5 @@ hName: 'figcaption' | ||
node.data = figure.data; | ||
if (!firstChild.value) { | ||
if (followingNodes.length) { | ||
parent.children.splice(index + 1, 1, { type: 'paragraph', children: followingNodes }); | ||
} else { | ||
parent.children.splice(index + 1, 1); | ||
@@ -101,0 +158,0 @@ } |
{ | ||
"name": "remark-captions", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"repository": { | ||
@@ -14,13 +14,7 @@ "url": "https://github.com/zestedesavoir/zmarkdown/tree/master/packages/remark-captions", | ||
], | ||
"nyc": { | ||
"exclude": [ | ||
"dist", | ||
"__tests__" | ||
] | ||
}, | ||
"scripts": { | ||
"pretest": "eslint src", | ||
"prepare": "del-cli dist && cross-env BABEL_ENV=production babel --out-dir dist src", | ||
"test": "npm run prepare && nyc ava __tests__", | ||
"coverage": "./node_modules/.bin/nyc report --reporter=text-lcov > coverage/coverage.lcov" | ||
"test": "jest", | ||
"coverage": "jest --coverage" | ||
}, | ||
@@ -44,9 +38,10 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"ava": "^0.19.1", | ||
"babel-cli": "^6.24.1", | ||
"babel-jest": "^20.0.3", | ||
"babel-preset-es2015": "^6.24.1", | ||
"cross-env": "^5.0.1", | ||
"dedent": "^0.7.0", | ||
"del-cli": "^1.0.0", | ||
"eslint": "^4.0.0", | ||
"nyc": "^11.0.2", | ||
"jest": "^20.0.4", | ||
"rehype-stringify": "^3.0.0", | ||
@@ -53,0 +48,0 @@ "remark-parse": "^3.0.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
8884
142
12