From c7fc7bec3c580eed3c2814e577b1d7ced2bb303c Mon Sep 17 00:00:00 2001 From: Hannu Pelkonen Date: Mon, 30 Mar 2015 20:06:55 +0300 Subject: [PATCH] Allow markdown and HTML in KSS header and description --- lib/app/views/partials/section.html | 2 +- lib/modules/kss-parser.js | 23 ++++++---- package.json | 2 +- test/unit/modules/kss-parser.test.js | 64 ++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 11 deletions(-) diff --git a/lib/app/views/partials/section.html b/lib/app/views/partials/section.html index 908d19d1..1689530a 100644 --- a/lib/app/views/partials/section.html +++ b/lib/app/views/partials/section.html @@ -5,7 +5,7 @@

{{ section.reference }} - {{ section.header }} + {{ section.file }}

diff --git a/lib/modules/kss-parser.js b/lib/modules/kss-parser.js index 082bfbfa..218587b2 100644 --- a/lib/modules/kss-parser.js +++ b/lib/modules/kss-parser.js @@ -6,15 +6,15 @@ var kss = require('kss'), kssSplitter = require('./kss-splitter'), kssAdditionalParams = require('./kss-additional-params'), kssSanitizeParams = require('./kss-sanitize-params'), - _ = require('lodash'), - sanitizeHtml = require('sanitize-html'); + marked = require('marked'), + _ = require('lodash'); // Parses kss.KssSection to JSON function jsonSections(sections) { return sections.map(function(section) { return { - header: section.header(), - description: sanitize(section.description()), + header: generateDescription(section.header(), {noWrapper: true}), + description: generateDescription(section.description()), modifiers: jsonModifiers(section.modifiers()), deprecated: section.deprecated(), experimental: section.experimental(), @@ -30,7 +30,7 @@ function jsonModifiers(modifiers) { return { id: id + 1, name: modifier.name(), - description: sanitize(modifier.description()), + description: modifier.description(), className: modifier.className(), markup: modifier.markup() ? modifier.markup().toString() : null }; @@ -45,12 +45,17 @@ function trimLinebreaks(str) { return str.replace(/^[\r\n]+|[\r\n]+$/g, ''); } -function sanitize(string) { +function generateDescription(string, options) { + var desc = marked(string); + if (options && options.noWrapper) { + // Remove wrapping p tags + desc = desc.replace(/^

/, ''); + desc = desc.replace(/<\/p>\n$/, ''); + } - var sanitized = sanitizeHtml(string, {allowedTags: [], allowedAttributes: []}); // HACK: Remove extra parameters from descriotion - sanitized = sanitized.split(/sg\-[^:]*:/)[0]; - return sanitized; + desc = desc.split(/sg\-[^:]*:/)[0]; + return desc; } function processBlock(block, options) { diff --git a/package.json b/package.json index ee145fa7..bb7b9bfb 100644 --- a/package.json +++ b/package.json @@ -45,12 +45,12 @@ "gulp-util": "~3.0.4", "kss": "~2.0.2", "lodash": "~3.5.0", + "marked": "^0.3.3", "minimatch": "~2.0.4", "morgan": "~1.5.2", "node-neat": "~1.7.1-beta1", "q": "~1.2.0", "run-sequence": "~1.0.2", - "sanitize-html": "~1.6.1", "socket.io": "~1.3.5", "through2": "~0.6.3", "vinyl": "~0.4.6", diff --git a/test/unit/modules/kss-parser.test.js b/test/unit/modules/kss-parser.test.js index 0e0c1d28..de610ebb 100644 --- a/test/unit/modules/kss-parser.test.js +++ b/test/unit/modules/kss-parser.test.js @@ -67,6 +67,70 @@ describe('KSS parser', function() { }).then(done).catch(done); }); + it('should parse markdown in header correctly', function(done) { + var files = { + 'file.less': multiline(function() { + /* + // This should be __strong__. + // + // Styleguide 1.0 + */ + }) + }; + parse(files).then(function(sections) { + expect(sections[0].header).to.eql('This should be strong.'); + }).then(done).catch(done); + }); + + it('should allow HTML in header', function(done) { + var files = { + 'file.less': multiline(function() { + /* + // This should be strong. + // + // Styleguide 1.0 + */ + }) + }; + parse(files).then(function(sections) { + expect(sections[0].header).to.eql('This should be strong.'); + }).then(done).catch(done); + }); + + it('should parse markdown in description correctly', function(done) { + var files = { + 'file.less': multiline(function() { + /* + // Header + // + // This should be __strong__. + // + // Styleguide 1.0 + */ + }) + }; + parse(files).then(function(sections) { + expect(sections[0].description).to.eql('

This should be strong.

\n'); + }).then(done).catch(done); + }); + + it('should allow HTML in description', function(done) { + var files = { + 'file.less': multiline(function() { + /* + // Header + // + // This should be strong. + // + // Styleguide 1.0 + */ + }) + }; + parse(files).then(function(sections) { + expect(sections[0].description).to.eql('

This should be strong.

\n'); + }).then(done).catch(done); + }); + it('sorts sections numerically according to first level', function(done) { var file = {'file1.less': multiline(function() { /*