remark-lint
Advanced tools
Comparing version 5.3.0 to 5.4.0
@@ -34,2 +34,3 @@ /* This file is generated. */ | ||
'no-emphasis-as-heading': require('./rules/no-emphasis-as-heading.js'), | ||
'no-empty-url': require('./rules/no-empty-url.js'), | ||
'no-file-name-articles': require('./rules/no-file-name-articles.js'), | ||
@@ -42,2 +43,3 @@ 'no-file-name-consecutive-dashes': require('./rules/no-file-name-consecutive-dashes.js'), | ||
'no-heading-indent': require('./rules/no-heading-indent.js'), | ||
'no-heading-like-paragraph': require('./rules/no-heading-like-paragraph.js'), | ||
'no-heading-punctuation': require('./rules/no-heading-punctuation.js'), | ||
@@ -44,0 +46,0 @@ 'no-html': require('./rules/no-html.js'), |
@@ -42,6 +42,27 @@ /** | ||
* | ||
* 3:5: Cell should be padded, isn’t | ||
* 3:9: Cell should be padded, isn’t | ||
* 3:16: Cell should be padded, isn’t | ||
* 5:5: Cell should be padded with 1 space, not 3 | ||
* 5:10: Cell should be padded | ||
* 5:17: Cell should be padded | ||
* | ||
* @example {"name": "invalid.md", "label": "input", "setting": "padded"} | ||
* | ||
* | A | B | | ||
* | :----|----: | | ||
* | Alpha|Bravo | | ||
* | ||
* @example {"name": "invalid.md", "label": "output", "setting": "padded"} | ||
* | ||
* 3:8: Cell should be padded | ||
* 3:9: Cell should be padded | ||
* | ||
* @example {"name": "invalid.md", "label": "input", "setting": "compact"} | ||
* | ||
* |A | B| | ||
* |:----|-----:| | ||
* |Alpha|Bravo | | ||
* | ||
* @example {"name": "invalid.md", "label": "output", "setting": "compact"} | ||
* | ||
* 3:13: Cell should be compact | ||
* | ||
* @example {"name": "invalid.md", "label": "output", "setting": "invalid", "config": {"positionless": true}} | ||
@@ -51,9 +72,17 @@ * | ||
* | ||
* @example {"name": "empty.md"} | ||
* @example {"name": "empty-heading.md"} | ||
* | ||
* <!-- Empty cells are always OK. --> | ||
* <!-- Empty heading cells are always OK. --> | ||
* | ||
* | Alpha | | | ||
* | | Alpha | | ||
* | ----- | ------- | | ||
* | Bravo | Charlie | | ||
* | ||
* @example {"name": "empty-body.md"} | ||
* | ||
* <!-- Empty body cells are always OK. --> | ||
* | ||
* | Alpha | Bravo | | ||
* | ------- | ------- | | ||
* | Charlie | | | ||
*/ | ||
@@ -65,3 +94,2 @@ | ||
/* Dependencies. */ | ||
var visit = require('unist-util-visit'); | ||
@@ -71,10 +99,7 @@ var position = require('unist-util-position'); | ||
/* Expose. */ | ||
module.exports = tableCellPadding; | ||
/* Methods. */ | ||
var start = position.start; | ||
var end = position.end; | ||
/* Valid styles. */ | ||
var STYLES = { | ||
@@ -86,13 +111,3 @@ null: true, | ||
/** | ||
* Warn when table cells are incorrectly padded. | ||
* | ||
* @param {Node} ast - Root node. | ||
* @param {File} file - Virtual file. | ||
* @param {string?} preferred - Either `padded` (for | ||
* at least a space), `compact` (for no spaces when | ||
* possible), or `consistent`, which defaults to the | ||
* first found style. | ||
*/ | ||
function tableCellPadding(ast, file, preferred) { | ||
function tableCellPadding(tree, file, preferred) { | ||
preferred = typeof preferred !== 'string' || preferred === 'consistent' ? null : preferred; | ||
@@ -104,13 +119,14 @@ | ||
visit(ast, 'table', function (node) { | ||
var children = node.children; | ||
var contents = file.toString(); | ||
visit(tree, 'table', visitor); | ||
return; | ||
function visitor(node) { | ||
var rows = node.children; | ||
var contents = String(file); | ||
var starts = []; | ||
var ends = []; | ||
var cells; | ||
var locations; | ||
var positions; | ||
var cells = []; | ||
var style; | ||
var type; | ||
var warning; | ||
var sizes; | ||
@@ -121,86 +137,109 @@ if (generated(node)) { | ||
/** | ||
* Check a fence. Checks both its initial spacing | ||
* (between a cell and the fence), and its final | ||
* spacing (between the fence and the next cell). | ||
* | ||
* @param {number} initial - Starting index. | ||
* @param {number} final - Closing index. | ||
* @param {Node} cell - Table cell. | ||
* @param {Node?} next - Following cell. | ||
* @param {number} index - Position of `cell` in | ||
* its parent. | ||
*/ | ||
function check(initial, final, cell, next, index) { | ||
rows.forEach(eachRow); | ||
sizes = inferSizes(node); | ||
if (preferred === 'padded') { | ||
style = 1; | ||
} else if (preferred === 'compact') { | ||
style = 0; | ||
} else { | ||
style = null; | ||
starts.concat(ends).some(inferStyle); | ||
} | ||
cells.forEach(checkCell); | ||
return; | ||
function eachRow(row) { | ||
var children = row.children; | ||
check(start(row).offset, start(children[0]).offset, null, children[0]); | ||
ends.pop(); /* Ignore end before row. */ | ||
children.forEach(eachCell); | ||
starts.pop(); /* Ignore start after row */ | ||
function eachCell(cell, index) { | ||
var next = children[index + 1] || null; | ||
check(end(cell).offset, start(next).offset || end(row).offset, cell, next); | ||
cells.push(cell); | ||
} | ||
} | ||
function inferStyle(pos) { | ||
if (pos === undefined) { | ||
return false; | ||
} | ||
style = Math.min(pos, 1); | ||
return true; | ||
} | ||
function check(initial, final, prev, next) { | ||
var fence = contents.slice(initial, final); | ||
var pos = fence.indexOf('|'); | ||
if ( | ||
cell && | ||
pos !== -1 && | ||
(ends[index] === undefined || pos < ends[index]) | ||
) { | ||
ends[index] = pos; | ||
ends.push(prev && pos !== -1 && prev.children.length !== 0 ? pos : undefined); | ||
starts.push(next && next.children.length !== 0 ? fence.length - pos - 1 : undefined); | ||
} | ||
function checkCell(cell, index) { | ||
/* Ignore, when compact, every cell except the biggest in the column. */ | ||
if (style === 0 && size(cell) < sizes[index % sizes.length]) { | ||
return; | ||
} | ||
if (next && pos !== -1) { | ||
pos = fence.length - pos - 1; | ||
if (starts[index + 1] === undefined || pos < starts[index + 1]) { | ||
starts[index + 1] = pos; | ||
} | ||
} | ||
checkSide('start', cell, starts[index], index); | ||
checkSide('end', cell, ends[index]); | ||
} | ||
children.forEach(function (row) { | ||
var cells = row.children; | ||
function checkSide(side, cell, spacing, index) { | ||
var message; | ||
check(start(row).offset, start(cells[0]).offset, null, cells[0], -1); | ||
if (spacing === undefined || spacing === style) { | ||
return; | ||
} | ||
cells.forEach(function (cell, index) { | ||
var next = cells[index + 1] || null; | ||
var final = start(next).offset || end(row).offset; | ||
message = 'Cell should be '; | ||
check(end(cell).offset, final, cell, next, index); | ||
}); | ||
}); | ||
if (style === 0) { | ||
message += 'compact'; | ||
} else { | ||
message += 'padded'; | ||
positions = starts.concat(ends); | ||
if (spacing > style) { | ||
message += ' with 1 space, not ' + spacing; | ||
if (preferred === 'padded') { | ||
style = 1; | ||
} else if (preferred === 'compact') { | ||
style = 0; | ||
} else { | ||
style = null; | ||
/* May be right or center aligned. */ | ||
if (size(cell) < sizes[index % sizes.length]) { | ||
return; | ||
} | ||
} | ||
} | ||
positions.some(function (pos) { | ||
/* `some` skips non-existant indices, so | ||
* there's no need to check for `!isNaN`. */ | ||
style = Math.min(pos, 1); | ||
return true; | ||
}); | ||
file.message(message, cell.position[side]); | ||
} | ||
} | ||
} | ||
cells = children[0].children; | ||
function inferSizes(tree) { | ||
var sizes = Array(tree.align.length); | ||
locations = cells.map(function (cell) { | ||
return start(cell); | ||
}).concat(cells.map(function (cell) { | ||
return end(cell); | ||
})); | ||
tree.children.forEach(row); | ||
cells = cells.concat(cells); | ||
type = style === 1 ? 'padded' : 'compact'; | ||
warning = 'Cell should be ' + type + ', isn’t'; | ||
return sizes; | ||
positions.forEach(function (diff, index) { | ||
var cell = cells[index]; | ||
function row(node) { | ||
node.children.forEach(cell); | ||
} | ||
if (cell && cell.children.length !== 0 && diff !== style && diff !== undefined && diff !== null) { | ||
file.message(warning, locations[index]); | ||
} | ||
}); | ||
}); | ||
function cell(node, index) { | ||
sizes[index] = Math.max(sizes[index] || 0, size(node)); | ||
} | ||
} | ||
function size(node) { | ||
return end(node).offset - start(node).offset; | ||
} |
{ | ||
"name": "remark-lint", | ||
"version": "5.3.0", | ||
"version": "5.4.0", | ||
"description": "Lint markdown with remark", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
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
140493
66
5161