From 50de3f5094e1886cd36ada33c72e383af7bad2dc Mon Sep 17 00:00:00 2001 From: PreslavKozovski Date: Wed, 10 Feb 2021 15:01:40 +0200 Subject: [PATCH] feat(parsers): allow usage of multiple example annotations --- dss.js | 12 ++++++----- test/data/css/multiple-examples.scss | 11 ++++++++++ test/data/scss/multiple-examples.scss | 10 +++++++++ test/test.js | 29 +++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 test/data/css/multiple-examples.scss create mode 100644 test/data/scss/multiple-examples.scss diff --git a/dss.js b/dss.js index 260c2fe..eec4162 100644 --- a/dss.js +++ b/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); } }); diff --git a/test/data/css/multiple-examples.scss b/test/data/css/multiple-examples.scss new file mode 100644 index 0000000..7a9efe2 --- /dev/null +++ b/test/data/css/multiple-examples.scss @@ -0,0 +1,11 @@ +/** + * @example + * + * + * + * + * @example + *
+ * This is a span + *
+ */ diff --git a/test/data/scss/multiple-examples.scss b/test/data/scss/multiple-examples.scss new file mode 100644 index 0000000..3de31d4 --- /dev/null +++ b/test/data/scss/multiple-examples.scss @@ -0,0 +1,10 @@ +/// @example +/// +/// +/// +/// +/// @example +///
+/// This is a span +///
+/// diff --git a/test/test.js b/test/test.js index 2f69c31..31c111d 100644 --- a/test/test.js +++ b/test/test.js @@ -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, + ' \n' + + ' \n' + + ' ' + ); + assert.strictEqual(data.example[0].escaped, + ' <span>\n' + + ' <button>This is a button</button>\n' + + ' </span>' + ); + assert.strictEqual(data.example[1].example, + '
\n' + + ' This is a span\n' + + '
' + ); + assert.strictEqual(data.example[1].escaped, + ' <div>\n' + + ' <span>This is a span</span>\n' + + ' </div>' + ); + }); + }); + }); });