Skip to content

Commit 620b507

Browse files
authored
Merge pull request #206 from ember-learn/add-feature-flags
Add a showdown extension to strip non-enabled feature flags. Feature …
2 parents 47237c1 + ce46ca4 commit 620b507

File tree

6 files changed

+86
-4
lines changed

6 files changed

+86
-4
lines changed

guidemaker-ember-template/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@
9696
"./components/table-of-contents.js": "./dist/_app_/components/table-of-contents.js",
9797
"./helpers/html-safe.js": "./dist/_app_/helpers/html-safe.js",
9898
"./helpers/starts-with.js": "./dist/_app_/helpers/starts-with.js",
99-
"./initializers/ember-template-showdown-extensions.js": "./dist/_app_/initializers/ember-template-showdown-extensions.js",
99+
"./instance-initializers/ember-template-showdown-extensions.js": "./dist/_app_/instance-initializers/ember-template-showdown-extensions.js",
100100
"./modifiers/highlight-active-title.js": "./dist/_app_/modifiers/highlight-active-title.js",
101+
"./services/features.js": "./dist/_app_/services/features.js",
101102
"./services/search.js": "./dist/_app_/services/search.js",
102103
"./templates/application.js": "./dist/_app_/templates/application.js",
103104
"./templates/error.js": "./dist/_app_/templates/error.js",

guidemaker-ember-template/rollup.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default {
3131
'modifiers/**/*.js',
3232
'services/**/*.js',
3333
'templates/**/*.js',
34-
'initializers/**/*.js',
34+
'instance-initializers/**/*.js',
3535
]),
3636

3737
// Follow the V2 Addon rules about dependencies. Your code can import from

guidemaker-ember-template/src/components/guides-article.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666

6767
<MarkdownToHtml
6868
@markdown={{@model.content}}
69-
@extensions="showdown-section-groups header-links"
69+
@extensions="showdown-section-groups header-links feature-flags"
7070
/>
7171

7272
<ChapterLinks />

guidemaker-ember-template/src/initializers/ember-template-showdown-extensions.js renamed to guidemaker-ember-template/src/instance-initializers/ember-template-showdown-extensions.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import showdown from 'showdown';
22

3-
export function initialize(/* application */) {
3+
export function initialize(application) {
44
showdown.extension('header-links', function () {
55
return [
66
{
@@ -12,6 +12,38 @@ export function initialize(/* application */) {
1212
},
1313
];
1414
});
15+
16+
const features = application.lookup('service:features');
17+
showdown.extension('feature-flags', function () {
18+
return [
19+
{
20+
type: 'lang',
21+
filter: function (text) {
22+
return text
23+
.replace(
24+
/<feature-flag-on-([^>]+)>([\s\S]*?)<\/feature-flag-on-\1>/g,
25+
function (match, featureName, content) {
26+
if (features.isEnabled(featureName)) {
27+
return content;
28+
} else {
29+
return '';
30+
}
31+
},
32+
)
33+
.replace(
34+
/<feature-flag-off-([^>]+)>([\s\S]*?)<\/feature-flag-off-\1>/g,
35+
function (match, featureName, content) {
36+
if (!features.isEnabled(featureName)) {
37+
return content;
38+
} else {
39+
return '';
40+
}
41+
},
42+
);
43+
},
44+
},
45+
];
46+
});
1547
}
1648

1749
export default {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Service from '@ember/service';
2+
3+
export default class Features extends Service {
4+
features = {};
5+
setupFeatures(features) {
6+
this.features = features;
7+
}
8+
9+
isEnabled(featureName) {
10+
return this.features[featureName];
11+
}
12+
}

test-app/tests/integration/components/guides-article-test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,41 @@ module('Integration | Component | guides-article', function (hooks) {
9898
'https://github.com/ember-learn/guidemaker-ember-template/edit/master/guides/v1.0.0/model-id.md',
9999
);
100100
});
101+
102+
test('it renders content based on feature flags', async function (assert) {
103+
this.owner.register('service:page', PageStub);
104+
this.owner.lookup('service:features').setupFeatures({
105+
'template-tag': true,
106+
});
107+
108+
this.model = {
109+
id: 'model-id',
110+
content: `
111+
<feature-flag-on-template-tag>
112+
This is template tag content.
113+
</feature-flag-on-template-tag>
114+
115+
<feature-flag-off-template-tag>
116+
This is classic template content.
117+
</feature-flag-off-template-tag>`,
118+
};
119+
120+
await render(hbs`
121+
<GuidesArticle @model={{this.model}} @version="v1.0.0" />
122+
`);
123+
124+
assert
125+
.dom('.guides-article-content')
126+
.includesText('This is template tag content.');
127+
assert
128+
.dom('.guides-article-content')
129+
.doesNotIncludeText('This is classic template content.');
130+
131+
assert
132+
.dom('feature-flag-off-template-tag')
133+
.doesNotExist('Feature flag tags are stripped');
134+
assert
135+
.dom('feature-flag-on-template-tag')
136+
.doesNotExist('Feature flag tags are stripped');
137+
});
101138
});

0 commit comments

Comments
 (0)