Skip to content
This repository was archived by the owner on Oct 22, 2019. It is now read-only.

Commit 4292de4

Browse files
fstanissebastianbenz
authored andcommitted
add @filter syntax (#2037)
1 parent e9d8b7a commit 4292de4

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

lib/CodeSection.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@ module.exports = class CodeSection {
7676
this.storyPageId = '';
7777
this.currentHint = '';
7878
this.hints = [];
79+
this.filters = null;
7980
}
8081

8182
appendDoc(doc) {
8283
doc = doc.replace(/<!--|-->/gm, '') + '\n';
84+
doc = this.parseFilters(doc);
8385
this.extractHeadings(doc);
8486
this.doc_ += doc;
8587
this.cachedMarkedDoc = false;
@@ -238,6 +240,18 @@ module.exports = class CodeSection {
238240
return lines.slice(1, lines.length-1).join('\n');
239241
}
240242

243+
parseFilters(line) {
244+
const filterMatch = line.match(/@filter\(([^)]+)\)/);
245+
if (!filterMatch) {
246+
return line;
247+
}
248+
if (this.filters) {
249+
throw new Error('Multiple @filter annotations found in section');
250+
}
251+
this.filters = filterMatch[1].split(',').map(f => f.trim());
252+
return line.replace(filterMatch[0], '');
253+
}
254+
241255
extractHeadings(line) {
242256
const matches = line.match(/^\s*#+\s*(.+)$/m);
243257
if (!matches) {

spec/compiler/CodeSectionSpec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ describe("CodeSection", function() {
8484
expect(section.doc).toEqual("\nhello\n\n\nworld\n\n");
8585
});
8686

87+
it("parses filters", function() {
88+
section.appendDoc("<!--hello");
89+
section.appendDoc("world! @filter(websites, email)-->");
90+
expect(section.doc).toEqual("hello\nworld! \n");
91+
expect(section.filters).toEqual(["websites", "email"]);
92+
});
93+
8794
});
8895
describe('hide columns if code section', function() {
8996
/*

0 commit comments

Comments
 (0)