Skip to content

Commit 0ae6d42

Browse files
authored
refactor: allow remark-lint-expected-html-sections to be disabled
PR-URL: #11009 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 23a6428 commit 0ae6d42

File tree

1 file changed

+44
-2
lines changed
  • lib/node_modules/@stdlib/_tools/remark/plugins/remark-lint-expected-html-sections/lib

1 file changed

+44
-2
lines changed

lib/node_modules/@stdlib/_tools/remark/plugins/remark-lint-expected-html-sections/lib/linter.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,44 @@ var keys = require( '@stdlib/utils/keys' );
3030
var debug = logger( 'remark-lint-expected-html-sections' );
3131
var RE_SECTION_START = /<section(?:\s+class="([^"]*)")?>/;
3232
var RE_SECTION_END = /<\/section>/;
33+
var RE_HTML_COMMENT = /^\s*<!--[\s\S]*-->\s*$/;
34+
35+
36+
// FUNCTIONS //
37+
38+
/**
39+
* Returns a message anchor node.
40+
*
41+
* @private
42+
* @param {Node} tree - abstract syntax tree (AST)
43+
* @returns {Node} anchor node
44+
*/
45+
function getAnchorNode( tree ) {
46+
var node;
47+
var i;
48+
49+
for ( i = 0; i < tree.children.length; i++ ) {
50+
node = tree.children[ i ];
51+
if ( node.type === 'html' && RE_HTML_COMMENT.test( node.value ) ) {
52+
continue;
53+
}
54+
return node;
55+
}
56+
return tree;
57+
}
58+
59+
/**
60+
* Reports a lint message.
61+
*
62+
* @private
63+
* @param {File} file - virtual file
64+
* @param {Node} node - anchor node
65+
* @param {string} msg - message
66+
* @returns {void}
67+
*/
68+
function reportMessage( file, node, msg ) {
69+
file.message( msg, node, 'remark-lint:expected-html-sections' );
70+
}
3371

3472

3573
// MAIN //
@@ -60,6 +98,7 @@ function factory( options ) {
6098
var sectionsFound;
6199
var sectionStack;
62100
var missingRoot;
101+
var anchorNode;
63102
var missingC;
64103
var schema;
65104
var msg;
@@ -78,6 +117,9 @@ function factory( options ) {
78117
'c': {}
79118
};
80119

120+
// Anchor messages to content nodes so inline lint comments can control reporting:
121+
anchorNode = getAnchorNode( tree );
122+
81123
// Visit all HTML nodes to build section structure:
82124
visit( tree, 'html', visitor );
83125

@@ -101,7 +143,7 @@ function factory( options ) {
101143
if ( missingRoot.length > 0 ) {
102144
msg = 'Missing required root-level sections: `' + missingRoot.join( '`, `' ) + '`. Required sections are: `' + requiredRootSections.join( '`, `' ) + '`. missing-required-sections';
103145
debug( msg );
104-
file.message( msg, tree, 'remark-lint:expected-html-sections' );
146+
reportMessage( file, anchorNode, msg );
105147
}
106148

107149
// If 'c' section exists, check its requirements:
@@ -119,7 +161,7 @@ function factory( options ) {
119161
if ( missingC.length > 0 ) {
120162
msg = 'Missing required sections in "c" section: `' + missingC.join( '`, `' ) + '`. Required C sections are: `' + requiredCSections.join( '`, `' ) + '`. missing-required-c-sections';
121163
debug( msg );
122-
file.message( msg, sectionsFound.root.c.node, 'remark-lint:expected-html-sections' );
164+
reportMessage( file, sectionsFound.root.c.node, msg );
123165
}
124166
}
125167

0 commit comments

Comments
 (0)