Skip to content

Commit b06a554

Browse files
committed
Reflect snippets changes to doc (md) files
1 parent a137c98 commit b06a554

File tree

6 files changed

+138
-25
lines changed

6 files changed

+138
-25
lines changed

README.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,40 @@ Use the `<snippet id='<your-snippet-id>'/>` notation to define the corresponding
7676
<snippet id='sum-snippet'/>
7777
```
7878

79+
After the first generation your snippets will be wrapped around the snippet notation you have been provided. This way when you update your snipet source - the `markdown-snippet-injector` will reflect the changes in your markdown as well.
80+
81+
Example:
82+
83+
main.css
84+
```
85+
/* >> css-snippet */
86+
.btn {
87+
color: green;
88+
text-align: center;
89+
}
90+
/* << css-snippet */
91+
```
92+
93+
README.MD
94+
```
95+
This is a CSS snippet
96+
<snippet id='css-snippet'/>
97+
```
98+
99+
After first build the README.MD will looks like:
100+
```
101+
This is a CSS snippet
102+
<snippet id='css-snippet'>
103+
```
104+
.btn {
105+
color: green;
106+
text-align: center;
107+
}
108+
```
109+
</snippet>
110+
```
111+
Then when you update `main.css`, your README.MD will be updated as well.
112+
79113
# Advanced features
80114
## Nested snippets
81115
Nested snippets are also supported. This is helpful in scenarios where you want to explain parts of a larger snippet in steps:
@@ -162,12 +196,11 @@ When injected, a snippet is formatted using the default MarkDown code-snippet fo
162196
```
163197
mdinject --root=. --docsroot=../ --sourceext=".java|.cs" --targetext=".md|.txt" --snippettitles="Java|C#"
164198
```
199+
> Note that the order of the snippet titles must be the related to the order of the source extension types so that they match.
165200
166201
## Run e2e tests
167202
1. Clone repo
168203
2. npm install
169204
3. npm test
170205

171206
E2E tests are developed with [Mocha](https://mochajs.org).
172-
173-
> Note that the order of the snippet titles must be the related to the order of the source extension types so that they match.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
"scripts": {
1414
"prepublish": "tsc -p .",
15-
"test": "mocha"
15+
"test": "tsc && mocha"
1616
},
1717
"keywords": [
1818
"markdown",

snippet-injector.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const cssSpec: FormatSpec = {
3232
const xmlSpec: FormatSpec = {
3333
commentStart: ws + "<!--" + ws,
3434
commentEnd: ws + "-->" + wsAndLine,
35-
postProcess: function(snippet: string) {
35+
postProcess: function (snippet: string) {
3636
var bindingRegEx = new RegExp("\{\{.*\}\}");
3737
var newLineChar = '\n';
3838
var linesOfSnippet = snippet.split(newLineChar);
@@ -112,7 +112,7 @@ export class SnippetInjector {
112112
this._storedSourceTitles[this._storedSourceTypes[i]] = (currentTitles[i] || "")
113113
}
114114
}
115-
115+
116116
this._fileFormatSpecs['.cs'] = jsSpec;
117117
this._fileFormatSpecs['.swift'] = jsSpec;
118118
this._fileFormatSpecs['.h'] = jsSpec;
@@ -172,9 +172,25 @@ export class SnippetInjector {
172172
}
173173
}
174174

175+
private replaceWrappedSnippetsWithCorespondingTags(fileContent): string {
176+
var content = "";
177+
content = fileContent.replace(/\<snippet id=['"]([a-zA-Z0-9\-]+)[\S\s]>[\S\s]*?<\/snippet>/g, "<snippet id='$1'/>");
178+
return content;
179+
}
180+
181+
private wrapSnippetWithComments(snippetTag, snippetId): string {
182+
var wrappedSnippetTag = "";
183+
wrappedSnippetTag += "\n<snippet id='" + snippetId + "'>\n"
184+
wrappedSnippetTag += snippetTag
185+
wrappedSnippetTag += "\n</snippet>\n"
186+
187+
return wrappedSnippetTag;
188+
}
189+
175190
private processDocsFile(path: string, extensionFilter: string) {
176191
console.log("Processing docs file: " + path);
177192
var fileContents = fsModule.readFileSync(path, 'utf8');
193+
fileContents = this.replaceWrappedSnippetsWithCorespondingTags(fileContents);
178194
var regExpOpen = /\<\s*snippet\s+id=\'((?:[a-z]+\-)*[a-z]+)\'\s*\/\s*\>/g;
179195
var match = regExpOpen.exec(fileContents);
180196
var hadMatches: boolean = false;
@@ -197,6 +213,8 @@ export class SnippetInjector {
197213
}
198214

199215
if (finalSnippet.length > 0) {
216+
var tmpMatchedString = this.wrapSnippetWithComments(matchedString, placeholderId);
217+
fileContents = fileContents.replace(matchedString, tmpMatchedString);
200218
fileContents = fileContents.replace(matchedString, finalSnippet);
201219
console.log("Token replaced: " + matchedString);
202220
}

test/docsroot/test.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,19 @@ This one has some hidden parts:
1010
This one has some hidden parts:
1111
<snippet id='ts-snippet-with-hidden-section'/>
1212

13-
1413
# CSS snippet:
1514
<snippet id='css-snippet'/>
1615
<snippet id='cssSnippet'/>
1716

1817
This one has some hidden parts:
1918
<snippet id='css-snippet-with-hidden-section'/>
19+
20+
This one is already processed snippet
21+
<snippet id='css-already-processed'>
22+
```
23+
.btn {
24+
color: green;
25+
background-color: blue;
26+
}
27+
```
28+
</snippet>

test/e2e.js

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,79 @@ function hasPattern(pattern, shouldExists, callback) {
2121
} else {
2222
callback(shouldExists ? null : 'Pattern ' + pattern + ' WAS found. This is NOT expected.');
2323
}
24-
2524
}
2625
});
2726
}
2827

28+
function contain(pattern, callback) {
29+
hasPattern(pattern, true, callback);
30+
}
31+
32+
function notContain(pattern, callback) {
33+
hasPattern(pattern, false, callback);
34+
}
35+
2936
describe('markdown-snippet-injector', function () {
3037

3138
beforeEach(function (done) {
3239
preparedemo();
3340
done();
3441
});
3542

36-
//TODO: Add tests for hidden fields
43+
describe('XML',
44+
function () {
45+
it('should process XML snippets', function (done) {
46+
shelljs.exec('node index.js --root=./test/root --docsroot=./test/docsroot-output --sourceext=".xml"');
47+
notContain("<snippet id='xml-snippet'/>", function () {
48+
contain("<snippet id='xml-snippet'>", function () {
49+
contain("</snippet>", done);
50+
});
51+
});
52+
});
53+
});
3754

38-
it('should process XML snippets', function (done) {
39-
shelljs.exec('node index.js --root=./test/root --docsroot=./test/docsroot-output --sourceext=".xml"');
40-
hasPattern("<snippet id='xml-snippet'/>", false, done);
41-
});
55+
describe('TypeScript',
56+
function () {
57+
it('should process TypeScript snippets', function (done) {
58+
shelljs.exec('node index.js --root=./test/root --docsroot=./test/docsroot-output --sourceext=".ts"');
59+
notContain("<snippet id='ts-snippet'/>", function () {
60+
contain("<snippet id='ts-snippet'>", function () {
61+
contain("</snippet>", done);
62+
});
63+
});
64+
});
65+
});
4266

43-
it('should process TypeScript snippets', function (done) {
44-
shelljs.exec('node index.js --root=./test/root --docsroot=./test/docsroot-output --sourceext=".ts"');
45-
hasPattern("<snippet id='ts-snippet'/>", false, done);
46-
});
67+
describe('CSS',
68+
function () {
69+
it('should process CSS snippets', function (done) {
70+
shelljs.exec('node index.js --root=./test/root --docsroot=./test/docsroot-output --sourceext=".css"');
71+
notContain("<snippet id='css-snippet'/>", function () {
72+
contain("<snippet id='css-snippet'>", function () {
73+
contain("</snippet>", done);
74+
});
75+
});
76+
});
4777

48-
it('should process CSS snippets', function (done) {
49-
shelljs.exec('node index.js --root=./test/root --docsroot=./test/docsroot-output --sourceext=".css"');
50-
hasPattern("<snippet id='css-snippet'/>", false, done);
51-
});
78+
it('should keep hidden the marked area in CSS', function (done) {
79+
shelljs.exec('node index.js --root=./test/root --docsroot=./test/docsroot-output --sourceext=".css"');
80+
notContain("visibility: hidden;", done);
81+
});
5282

53-
it('should NOT process snippetIds that are not defined in source', function (done) {
54-
shelljs.exec('node index.js --root=./test/root --docsroot=./test/docsroot-output --sourceext=".css"');
55-
hasPattern("<snippet id='cssSnippet'/>", true, done);
56-
});
83+
it('should NOT process snippetIds that are not defined in source', function (done) {
84+
shelljs.exec('node index.js --root=./test/root --docsroot=./test/docsroot-output --sourceext=".css"');
85+
contain("<snippet id='cssSnippet'/>", function () {
86+
notContain("<snippet id='cssSnippet'>", done);
87+
});
88+
});
89+
90+
it('should update the already processed snippet tags', function (done) {
91+
shelljs.exec('node index.js --root=./test/root --docsroot=./test/docsroot-output --sourceext=".css"');
92+
contain("<snippet id='css-already-processed'>", function () {
93+
contain("</snippet>", function(){
94+
contain("color: red;", done);
95+
});
96+
});
97+
});
98+
});
5799
});

test/root/test-css.css

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,19 @@
88
/* >> css-snippet-with-hidden-section */
99
.btn {
1010
color: green;
11+
background-color: blue;
1112
/* >> (hide) */
1213
visibility: hidden;
1314
/* << (hide) */
1415
}
15-
/* << css-snippet-with-hidden-section */
16+
/* << css-snippet-with-hidden-section */
17+
18+
/* >> css-already-processed */
19+
.text {
20+
color: red;
21+
font-size: 10px;
22+
/* >> (hide) */
23+
visibility: hidden;
24+
/* << (hide) */
25+
}
26+
/* << css-already-processed */

0 commit comments

Comments
 (0)