doc-snippets
Advanced tools
Comparing version 0.4.0-pre.3 to 0.4.0-pre.4
@@ -43,40 +43,25 @@ "use strict"; | ||
exports.injectSnippetsIntoFile = void 0; | ||
var regexp_1 = require("./utils/regexp"); | ||
var fs_1 = __importDefault(require("fs")); | ||
function injectSnippetsIntoFile(snippets, filePath, injectToken) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var contents, modified, index, snippetStartIdx, nameStartIdx, snippetEndIdx, firstSpaceIdx, name; | ||
var contents, modified, injectionRegExp, nextMatch, snippetName, snippetStartIdx, snippetEndIdx; | ||
return __generator(this, function (_a) { | ||
contents = fs_1.default.readFileSync(filePath, "utf-8"); | ||
modified = false; | ||
index = 0; | ||
while (index < contents.length) { | ||
snippetStartIdx = contents.indexOf(injectToken, index); | ||
if (snippetStartIdx < 0) { | ||
index = contents.length; | ||
continue; | ||
injectionRegExp = (0, regexp_1.getInjectionTokenCaptureRegExp)(injectToken); | ||
nextMatch = null; | ||
while ((nextMatch = contents.match(injectionRegExp))) { | ||
snippetName = nextMatch[1]; | ||
snippetStartIdx = nextMatch.index; | ||
snippetEndIdx = snippetStartIdx + nextMatch[0].length; | ||
if (!snippets[snippetName]) { | ||
throw Error("Unknown Snippet: ".concat(snippetName, " in ").concat(filePath)); | ||
} | ||
nameStartIdx = snippetStartIdx + injectToken.length; | ||
snippetEndIdx = contents.indexOf("\n", nameStartIdx); | ||
firstSpaceIdx = contents.indexOf(" ", nameStartIdx); | ||
// Either there's no newline, or the 1st space char appears earlier than the newline. | ||
if (snippetEndIdx == -1 || | ||
(firstSpaceIdx > 0 && snippetEndIdx > firstSpaceIdx)) { | ||
snippetEndIdx = firstSpaceIdx; | ||
} | ||
// If we're hitting the end of the string while searching for both a newline and a space, | ||
// the end of the snippet is at the end of the string. | ||
if (snippetEndIdx == -1) { | ||
snippetEndIdx = contents.length; | ||
} | ||
name = contents.substring(nameStartIdx, snippetEndIdx); | ||
if (!snippets[name]) { | ||
throw Error("Unknown Snippet: ".concat(name, " in ").concat(filePath)); | ||
} | ||
contents = | ||
contents.substring(0, snippetStartIdx) + | ||
snippets[name] + | ||
snippets[snippetName] + | ||
contents.substring(snippetEndIdx, contents.length); | ||
console.log("- Inject Snippet", name, "into", filePath); | ||
console.log("- Inject Snippet", snippetName, "into", filePath); | ||
modified = true; | ||
index = snippetStartIdx + snippets[name].length; | ||
} | ||
@@ -83,0 +68,0 @@ if (modified) { |
@@ -34,3 +34,2 @@ "use strict"; | ||
exports.getSnippetCaptureRegExp = getSnippetCaptureRegExp; | ||
var nameCapturePattern = "([\\w-]+)"; | ||
var contentCapturePattern = "(?<content>[\\s\\S]+?)"; | ||
@@ -75,3 +74,3 @@ var snippetNameToken = "{SNIPPET_NAME}"; | ||
function getRegularStartTokenPattern(token) { | ||
return "".concat((0, _1.escapeRegExp)(token)).concat(nameCapturePattern, ".*?\\n"); | ||
return "".concat((0, _1.escapeRegExp)(token)).concat(_1.snippetNameCapturePattern, ".*?\\n"); | ||
} | ||
@@ -86,3 +85,3 @@ function getRegularEndTokenPattern(token) { | ||
var tokenEnd = token.substring(nameEndIdx); | ||
return "".concat((0, _1.escapeRegExp)(tokenStart)).concat(nameCapturePattern).concat((0, _1.escapeRegExp)(tokenEnd)); | ||
return "".concat((0, _1.escapeRegExp)(tokenStart)).concat(_1.snippetNameCapturePattern).concat((0, _1.escapeRegExp)(tokenEnd)); | ||
} | ||
@@ -89,0 +88,0 @@ function getInlineEndTokenPattern(token) { |
export * from "./escapeRegExp"; | ||
export * from "./getSnippetCaptureRegExp"; | ||
export * from "./getInjectionCaptureRegExp"; | ||
export * from "./snippetNameCapturePattern"; |
@@ -19,2 +19,4 @@ "use strict"; | ||
__exportStar(require("./getSnippetCaptureRegExp"), exports); | ||
__exportStar(require("./getInjectionCaptureRegExp"), exports); | ||
__exportStar(require("./snippetNameCapturePattern"), exports); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "doc-snippets", | ||
"version": "0.4.0-pre.3", | ||
"version": "0.4.0-pre.4", | ||
"description": "Extract and inject snippets from code into markdown files", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -31,2 +31,8 @@ # doc-snippets | ||
### The snippet name | ||
The snippet name is a string containing **any characters except whitespace**. | ||
As soon as `doc-snippets` encounters any form of whitespace (space, tab, newline), the snippet name capture ends. | ||
## Configuration | ||
@@ -138,3 +144,3 @@ | ||
{ | ||
"pattern": "/* $start: {SNIPPET_NAME} */", | ||
"pattern": "/* #start: {SNIPPET_NAME} */", | ||
"inline": true | ||
@@ -145,3 +151,3 @@ } | ||
{ | ||
"pattern": "/* $end */", | ||
"pattern": "/* #end */", | ||
"inline": true | ||
@@ -158,4 +164,4 @@ } | ||
```typescript | ||
const greeting = /* $start: hello-inline-snippet */"Hello World!"; | ||
console.log(greeting);/* $end */ | ||
const greeting = /* #start: hello-inline-snippet */"Hello World!"; | ||
console.log(greeting);/* #end */ | ||
``` | ||
@@ -179,2 +185,36 @@ | ||
**:warning: Regular and inline snippets containing the same substring :warning:** | ||
If we have the following configuration for our end tokens: | ||
```json | ||
"endTokens": [ | ||
{ | ||
"pattern": "$end" | ||
} | ||
{ | ||
"pattern": "/* $end */", | ||
"inline": true | ||
} | ||
], | ||
``` | ||
the `$end` regular token will **always** match before the inline `/* $end */` token due to the way Regex matching works in Javascript. | ||
To avoid these collisions, make sure to specify inline extraction tokens which don't contain any of your regular injection tokens as exact substrings within them. | ||
An easy remedy for the above configuration is simply replacing `/* $end */` with `/* #end */`: | ||
```json | ||
"endTokens": [ | ||
{ | ||
"pattern": "$end" | ||
} | ||
{ | ||
"pattern": "/* #end */", | ||
"inline": true | ||
} | ||
], | ||
``` | ||
### `injectionToken` | ||
@@ -253,3 +293,3 @@ | ||
{ | ||
pattern: "/* $start: {SNIPPET_NAME} */", | ||
pattern: "/* #start: {SNIPPET_NAME} */", | ||
inline: true | ||
@@ -264,3 +304,3 @@ } | ||
{ | ||
pattern: "/* $end */", | ||
pattern: "/* #end */", | ||
inline: true | ||
@@ -267,0 +307,0 @@ } |
@@ -31,2 +31,8 @@ # doc-snippets | ||
### The snippet name | ||
The snippet name is a string containing **any characters except whitespace**. | ||
As soon as `doc-snippets` encounters any form of whitespace (space, tab, newline), the snippet name capture ends. | ||
## Configuration | ||
@@ -138,3 +144,3 @@ | ||
{ | ||
"pattern": "/* $start: {SNIPPET_NAME} */", | ||
"pattern": "/* #start: {SNIPPET_NAME} */", | ||
"inline": true | ||
@@ -145,3 +151,3 @@ } | ||
{ | ||
"pattern": "/* $end */", | ||
"pattern": "/* #end */", | ||
"inline": true | ||
@@ -158,4 +164,4 @@ } | ||
```typescript | ||
const greeting = /* $start: hello-inline-snippet */"Hello World!"; | ||
console.log(greeting);/* $end */ | ||
const greeting = /* #start: hello-inline-snippet */"Hello World!"; | ||
console.log(greeting);/* #end */ | ||
``` | ||
@@ -179,2 +185,36 @@ | ||
**:warning: Regular and inline snippets containing the same substring :warning:** | ||
If we have the following configuration for our end tokens: | ||
```json | ||
"endTokens": [ | ||
{ | ||
"pattern": "$end" | ||
} | ||
{ | ||
"pattern": "/* $end */", | ||
"inline": true | ||
} | ||
], | ||
``` | ||
the `$end` regular token will **always** match before the inline `/* $end */` token due to the way Regex matching works in Javascript. | ||
To avoid these collisions, make sure to specify inline extraction tokens which don't contain any of your regular injection tokens as exact substrings within them. | ||
An easy remedy for the above configuration is simply replacing `/* $end */` with `/* #end */`: | ||
```json | ||
"endTokens": [ | ||
{ | ||
"pattern": "$end" | ||
} | ||
{ | ||
"pattern": "/* #end */", | ||
"inline": true | ||
} | ||
], | ||
``` | ||
### `injectionToken` | ||
@@ -240,3 +280,3 @@ | ||
{ | ||
pattern: "/* $start: {SNIPPET_NAME} */", | ||
pattern: "/* #start: {SNIPPET_NAME} */", | ||
inline: true | ||
@@ -251,3 +291,3 @@ } | ||
{ | ||
pattern: "/* $end */", | ||
pattern: "/* #end */", | ||
inline: true | ||
@@ -254,0 +294,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
83653
72
308
942