remark-captions
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -5,12 +5,22 @@ 'use strict'; | ||
var visit = require('unist-util-visit'); | ||
var xtend = require('xtend'); | ||
function plugin() { | ||
var legendBlock = { | ||
table: 'Table:', | ||
code: 'Code:' | ||
}; | ||
function plugin(opts) { | ||
var blocks = xtend(legendBlock, opts || {}); | ||
return transformer; | ||
} | ||
function transformer(tree) { | ||
visit(tree, 'blockquote', visitor); | ||
function transformer(tree) { | ||
visit(tree, 'blockquote', internLegendVisitor); | ||
Object.keys(legendBlock).forEach(function (nodeType) { | ||
return visit(tree, nodeType, externLegendVisitorCreator(blocks)); | ||
}); | ||
} | ||
} | ||
function visitor(node, index, parent) { | ||
function internLegendVisitor(node, index, parent) { | ||
if (parent && parent.type === 'figure') return; | ||
@@ -25,3 +35,3 @@ var lastP = getLast(node.children); | ||
if (!lastLine) return; | ||
if (!lastLine.includes(':')) return; | ||
if (!lastLine.startsWith('Source:')) return; | ||
var legend = lines.pop().slice(lastLine.indexOf(':') + 1).trim(); | ||
@@ -55,2 +65,43 @@ | ||
function externLegendVisitorCreator(blocks) { | ||
return function (node, index, parent) { | ||
if (index + 1 < parent.children.length && parent.children[index + 1].type === 'paragraph') { | ||
var legendNode = parent.children[index + 1]; | ||
var firstChild = legendNode.children[0]; | ||
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; | ||
firstChild.value = firstChild.value.replace(fullLegendLine, '').trim(); | ||
var figcaption = { | ||
type: 'figcaption', | ||
children: [{ | ||
type: 'text', | ||
value: legendText | ||
}], | ||
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; | ||
if (!firstChild.value) { | ||
parent.children.splice(index + 1, 1); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
function getLast(xs) { | ||
@@ -57,0 +108,0 @@ var len = xs.length; |
{ | ||
"name": "remark-captions", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"repository": { | ||
@@ -22,4 +22,4 @@ "url": "https://github.com/zestedesavoir/zmarkdown/tree/master/packages/remark-captions", | ||
"pretest": "eslint src", | ||
"prepublish": "del-cli dist && BABEL_ENV=production babel --out-dir dist src", | ||
"test": "npm run prepublish && nyc ava __tests__", | ||
"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" | ||
@@ -42,3 +42,16 @@ }, | ||
"unist-util-visit": "^1.1.3" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.19.1", | ||
"babel-cli": "^6.24.1", | ||
"babel-preset-es2015": "^6.24.1", | ||
"cross-env": "^5.0.1", | ||
"del-cli": "^1.0.0", | ||
"eslint": "^4.0.0", | ||
"nyc": "^11.0.2", | ||
"rehype-stringify": "^3.0.0", | ||
"remark-parse": "^3.0.1", | ||
"remark-rehype": "^2.0.1", | ||
"unified": "^6.1.5" | ||
} | ||
} |
@@ -11,1 +11,52 @@ This plugin enhance mdast quotation to add the source of quotation. | ||
This removes the `Source` from the tree and add a `author` attribute to the blockquote element. | ||
This plugin also enables "external caption" which can be configured through the `opts` object passed to plugin. | ||
This object has to be formed as a dictionary associating the type of node to caption and the prefix. | ||
By default, it features : | ||
```javascript | ||
const legendBlock = { | ||
table: 'Table:', | ||
code: 'Code:', | ||
} | ||
``` | ||
This enables you to deal with such a code: | ||
``` | ||
a_highlighted_code('blah') | ||
``` | ||
Code: My code caption | ||
Table are also supported with such a code : | ||
```markdown | ||
head1| head2 | ||
-----|------ | ||
bla|bla | ||
Table: figcapt1 | ||
``` | ||
Associated with `remark-rehype` this generates a HTML tree encapsulated inside `<figure>` tag | ||
```html | ||
<figure> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>head1</th> | ||
<th>head2</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>bla</td> | ||
<td>bla</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
<figcaption>figcapt1</figcaption> | ||
</figure> | ||
``` |
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
6498
93
61
11