feat(parsers): allow usage of multiple example annotations

This commit is contained in:
PreslavKozovski 2021-02-10 15:01:40 +02:00
Родитель 04a154b9d3
Коммит 50de3f5094
4 изменённых файлов: 57 добавлений и 5 удалений

12
dss.js
Просмотреть файл

@ -214,9 +214,10 @@ let dss = ( function() {
* @param {string} line - The line to parse.
* @param {string} block - Parsed block.
* @param {string} file - Parsed file.
* @param {number} lineIndex - Index of the line.
* @returns {object} - Result of parsing.
*/
let parser = function( temp, line, block, file ) {
let parser = function( temp, line, block, file, lineIndex ) {
/* eslint-disable no-param-reassign */
let indexer = function( str, find ) {
return ( str.indexOf( find ) > 0 ) ? str.indexOf( find ) : false;
@ -226,14 +227,15 @@ let dss = ( function() {
let name = _dss.trim( parts.substr( 0, i ) );
let description = _dss.trim( parts.substr( i ) );
let variable = _dss.parsers[ name ];
let index = block.indexOf( line );
let restOfBlock = block.split( '\n' ).splice(lineIndex).join( '\n' );
let index = restOfBlock.indexOf( line );
if ( _dss.aliases[name] ) {
name = _dss.aliases[name];
}
line = {};
line[ name ] = ( variable ) ? variable.apply( null, [ index, description, block, file, name ] ) : ''; // eslint-disable-line no-useless-call
line[ name ] = ( variable ) ? variable.apply( null, [ index, description, restOfBlock, file, name ] ) : ''; // eslint-disable-line no-useless-call
if ( (overridableNames.indexOf(name) === -1) && temp[ name ] ) {
if ( !_dss.isArray( temp[ name ] ) ) {
@ -374,9 +376,9 @@ let dss = ( function() {
} ).join( '\n' );
// Split block into lines
block.split( '\n' ).forEach( function( line ) {
block.split( '\n' ).forEach( (line, lineIndex) => {
if ( _dss.detect( line ) ) {
temp = parser( temp, _dss.normalize( line ), block, lines );
temp = parser( temp, _dss.normalize( line ), block, lines, lineIndex);
}
});

Просмотреть файл

@ -0,0 +1,11 @@
/**
* @example
* <span>
* <button>This is a button</button>
* </span>
*
* @example
* <div>
* <span>This is a span</span>
* </div>
*/

Просмотреть файл

@ -0,0 +1,10 @@
/// @example
/// <span>
/// <button>This is a button</button>
/// </span>
///
/// @example
/// <div>
/// <span>This is a span</span>
/// </div>
///

Просмотреть файл

@ -159,6 +159,35 @@ const path = require('path');
});
});
it('multiple examples are correctly parsed', () => {
const file = fs.readFileSync(path.join(__dirname, `data/${ext}/multiple-examples.scss`), 'utf8');
dss.parse(file, {}, (parsed) => {
const data = parsed.blocks[0];
assert.strictEqual(data.example[0].example,
' <span>\n' +
' <button>This is a button</button>\n' +
' </span>'
);
assert.strictEqual(data.example[0].escaped,
' &lt;span&gt;\n' +
' &lt;button&gt;This is a button&lt;/button&gt;\n' +
' &lt;/span&gt;'
);
assert.strictEqual(data.example[1].example,
' <div>\n' +
' <span>This is a span</span>\n' +
' </div>'
);
assert.strictEqual(data.example[1].escaped,
' &lt;div&gt;\n' +
' &lt;span&gt;This is a span&lt;/span&gt;\n' +
' &lt;/div&gt;'
);
});
});
});
});