New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

doc-snippets

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

doc-snippets - npm Package Compare versions

Comparing version 0.4.0-pre.0 to 0.4.0-pre.1

build/cli/cli.d.ts

6

build/lib/combine.js

@@ -56,5 +56,5 @@ "use strict";

var inject_1 = require("./inject");
var utils_1 = require("./utils");
var fs_1 = __importDefault(require("fs"));
var path_1 = __importDefault(require("path"));
var utils_1 = require("./utils");
function combineDocsAndSnippets(config) {

@@ -68,3 +68,3 @@ return __awaiter(this, void 0, void 0, function () {

console.log("- Extract Snippets");
return [4 /*yield*/, (0, extract_1.extractSnippets)(config.extract)];
return [4 /*yield*/, (0, extract_1.extractSnippets)(config.extract, config.startToken, config.endToken)];
case 1:

@@ -117,3 +117,3 @@ snippets = _c.sent();

filePath = path_1.default.join(config.outputDir, file);
return [4 /*yield*/, (0, inject_1.injectSnippetsIntoFile)(snippets, filePath)];
return [4 /*yield*/, (0, inject_1.injectSnippetsIntoFile)(snippets, filePath, config.injectToken)];
case 12:

@@ -120,0 +120,0 @@ _c.sent();

@@ -15,4 +15,7 @@ "use strict";

},
outputDir: "./docs"
startToken: "$start: ",
endToken: "$end",
injectToken: "$snippet: ",
outputDir: "./docs",
};
//# sourceMappingURL=defaultDocSnippetsConfig.js.map

@@ -1,1 +0,1 @@

export * from './defaultDocSnippetsConfig';
export * from "./defaultDocSnippetsConfig";
import { SearchOptions } from "./types";
export declare function extractSnippets(options: SearchOptions): Promise<Record<string, string>>;
export declare function extractSnippetsFromFile(snippets: Record<string, string>, filePath: string): Promise<void>;
export declare function extractSnippets(options: SearchOptions, snippetStartToken: string, snippetEndToken: string): Promise<Record<string, string>>;
export declare function extractSnippetsFromFile(snippets: Record<string, string>, filePath: string, snippetStartToken: string, snippetEndToken: string): Promise<void>;

@@ -54,26 +54,42 @@ "use strict";

exports.extractSnippetsFromFile = exports.extractSnippets = void 0;
var utils_1 = require("./utils");
var fs_1 = __importDefault(require("fs"));
var path_1 = __importDefault(require("path"));
var utils_1 = require("./utils");
function extractSnippets(options) {
function extractSnippets(options, snippetStartToken, snippetEndToken) {
return __awaiter(this, void 0, void 0, function () {
var snippets, filePaths, filePaths_1, filePaths_1_1, filePath;
var snippets, filePaths, filePaths_1, filePaths_1_1, filePath, e_1_1;
var e_1, _a;
return __generator(this, function (_b) {
snippets = {};
filePaths = (0, utils_1.searchFiles)(options);
try {
for (filePaths_1 = __values(filePaths), filePaths_1_1 = filePaths_1.next(); !filePaths_1_1.done; filePaths_1_1 = filePaths_1.next()) {
switch (_b.label) {
case 0:
snippets = {};
filePaths = (0, utils_1.searchFiles)(options);
_b.label = 1;
case 1:
_b.trys.push([1, 6, 7, 8]);
filePaths_1 = __values(filePaths), filePaths_1_1 = filePaths_1.next();
_b.label = 2;
case 2:
if (!!filePaths_1_1.done) return [3 /*break*/, 5];
filePath = filePaths_1_1.value;
extractSnippetsFromFile(snippets, path_1.default.join(options.dir, filePath));
}
return [4 /*yield*/, extractSnippetsFromFile(snippets, path_1.default.join(options.dir, filePath), snippetStartToken, snippetEndToken)];
case 3:
_b.sent();
_b.label = 4;
case 4:
filePaths_1_1 = filePaths_1.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_1_1 = _b.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 8];
case 7:
try {
if (filePaths_1_1 && !filePaths_1_1.done && (_a = filePaths_1.return)) _a.call(filePaths_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 8: return [2 /*return*/, snippets];
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (filePaths_1_1 && !filePaths_1_1.done && (_a = filePaths_1.return)) _a.call(filePaths_1);
}
finally { if (e_1) throw e_1.error; }
}
return [2 /*return*/, snippets];
});

@@ -83,5 +99,5 @@ });

exports.extractSnippets = extractSnippets;
function extractSnippetsFromFile(snippets, filePath) {
function extractSnippetsFromFile(snippets, filePath, snippetStartToken, snippetEndToken) {
return __awaiter(this, void 0, void 0, function () {
var contents, index, start, end, startIdx, nameStartIdx, lineEndIdx, nameEndIdx, name, snippetStartIdx, snippetEndIdx, snippet;
var contents, index, startIdx, nameStartIdx, lineEndIdx, nameEndIdx, name, snippetStartIdx, snippetEndIdx, snippet;
return __generator(this, function (_a) {

@@ -91,5 +107,3 @@ contents = fs_1.default.readFileSync(filePath, "utf-8");

while (index < contents.length) {
start = "$start: ";
end = "$end";
startIdx = contents.indexOf(start, index);
startIdx = contents.indexOf(snippetStartToken, index);
if (startIdx < 0) {

@@ -99,3 +113,3 @@ index = contents.length;

}
nameStartIdx = startIdx + start.length;
nameStartIdx = startIdx + snippetStartToken.length;
lineEndIdx = contents.indexOf("\n", nameStartIdx);

@@ -108,3 +122,3 @@ nameEndIdx = contents.indexOf(" ", nameStartIdx);

snippetStartIdx = lineEndIdx + 1;
snippetEndIdx = contents.indexOf(end, snippetStartIdx);
snippetEndIdx = contents.indexOf(snippetEndToken, snippetStartIdx);
// Walk back from the $end until we hit the first \n

@@ -120,3 +134,3 @@ while (contents[snippetEndIdx] !== "\n") {

snippets[name] = snippet;
index = snippetEndIdx + end.length;
index = snippetEndIdx + snippetEndToken.length;
}

@@ -123,0 +137,0 @@ return [2 /*return*/];

@@ -1,1 +0,1 @@

export declare function injectSnippetsIntoFile(snippets: Record<string, string>, filePath: string): Promise<void>;
export declare function injectSnippetsIntoFile(snippets: Record<string, string>, filePath: string, injectToken: string): Promise<void>;

@@ -44,5 +44,5 @@ "use strict";

var fs_1 = __importDefault(require("fs"));
function injectSnippetsIntoFile(snippets, filePath) {
function injectSnippetsIntoFile(snippets, filePath, injectToken) {
return __awaiter(this, void 0, void 0, function () {
var contents, modified, index, marker, markerIdx, nameStartIdx, nameEndIdx, name;
var contents, modified, index, markerIdx, nameStartIdx, nameEndIdx, name;
return __generator(this, function (_a) {

@@ -53,4 +53,3 @@ contents = fs_1.default.readFileSync(filePath, "utf-8");

while (index < contents.length) {
marker = "$snippet: ";
markerIdx = contents.indexOf(marker, index);
markerIdx = contents.indexOf(injectToken, index);
if (markerIdx < 0) {

@@ -60,3 +59,3 @@ index = contents.length;

}
nameStartIdx = markerIdx + marker.length;
nameStartIdx = markerIdx + injectToken.length;
nameEndIdx = contents.indexOf("\n", nameStartIdx);

@@ -67,3 +66,3 @@ name = contents.substring(nameStartIdx, nameEndIdx);

}
contents = contents.replace("".concat(marker).concat(name, "\n"), "".concat(snippets[name], "\n"));
contents = contents.replace("".concat(injectToken).concat(name, "\n"), "".concat(snippets[name], "\n"));
console.log("- Inject Snippet", name, "into", filePath);

@@ -70,0 +69,0 @@ modified = true;

@@ -5,2 +5,5 @@ import { SearchOptions } from ".";

inject: SearchOptions;
startToken: string;
endToken: string;
injectToken: string;
outputDir: string;

@@ -11,3 +14,6 @@ };

inject?: Partial<SearchOptions>;
startToken?: string;
endToken?: string;
injectToken?: string;
outputDir?: string;
};

@@ -1,2 +0,2 @@

export * from './docSnippetsConfig';
export * from './searchOptions';
export * from "./docSnippetsConfig";
export * from "./searchOptions";

@@ -1,1 +0,1 @@

export * from './searchFiles';
export * from "./searchFiles";
{
"name": "doc-snippets",
"version": "0.4.0-pre.0",
"version": "0.4.0-pre.1",
"description": "Extract and inject snippets from code into markdown files",

@@ -29,3 +29,4 @@ "author": {

"scripts": {
"build": "npx rimraf ./build && tsc --project tsconfig.build.json",
"build": "npx rimraf ./build && tsc --project tsconfig.build.json && yarn build:readme",
"build:readme": "ts-node ./readme/build.ts",
"clean": "npx rimraf ./build ./node_modules",

@@ -37,6 +38,8 @@ "reset": "yarn clean && yarn && yarn build",

"publish:pre:npm": "yarn publish --access public --non-interactive --verbose --tag pre",
"version:apply": "yarn version --new-version $(cat VERSION) --no-git-tag-version"
"version:apply": "yarn version --new-version $(cat VERSION) --no-git-tag-version",
"test": "jest --passWithNoTests --runInBand --verbose"
},
"devDependencies": {
"@types/fs-extra": "^9.0.13",
"@types/jest": "^29.2.4",
"@types/rimraf": "3.0.2",

@@ -49,4 +52,7 @@ "@typescript-eslint/eslint-plugin": "^5.44.0",

"eslint-plugin-prettier": "^4.2.1",
"fs-extra": "^11.1.0",
"jest": "^29.3.1",
"prettier": "^2.8.0",
"rimraf": "3.0.2",
"ts-jest": "^29.0.3",
"ts-node": "10.7.0",

@@ -53,0 +59,0 @@ "typescript": "4.6.4"

@@ -49,2 +49,5 @@ # doc-snippets

},
"startToken": "$start: ",
"endToken": "$end",
"injectToken": "$snippet: ",
"outputDir": "./docs"

@@ -101,11 +104,17 @@ }

#### Options
#### 'combine' command options
- `-o, --output-dir <path>` - The output directory for injected documentation
- `-c --config <path>` - Path to configuration file (default: './package.json')
- `-o --output-dir <path>` - Combined documentation output directory
- `--extract-dir <path>` - The base directory within which to search for snippets
- `--extract-include <paths...>` - Include specified paths or glob patterns in snippet extraction
- `--extract-ignore <paths...>` - Ignore specified paths or glob patterns in snippet extraction
- `--inject-dir <path>` - The base directory within which to search for injectable files
- `--inject-include <paths...>` - Include specified paths or glob patterns in snippet injection
- `--inject-ignore <paths...>` - Ignore specified paths or glob patterns in snippet injection
- `--start-token <token>` - Token marking the start of the snippet
- `--end-token <token>` - Token marking the end of the snippet
- `--inject-token <token>` - Token marking the point of snippet injection
- By default, only `node_modules` is ignored.
- `-e, --exts <exts...>` - Parse files with listed extensions
- By default, files with the following extensions are parsed: `.ts`, `.json`, `.yaml`, `.txt`, `.md`, `.graphql`, and `.cue`
### In your own code

@@ -118,9 +127,13 @@

const snippets = await extractSnippets({
const searchOptions = {
include: ["sample.sql", "./**/*.{js,ts}"],
ignore: "./**/node_modules/**",
dir: "./src"
}); //Returns snippets as `Record<string, string>`.
}
await injectSnippetsIntoFile(snippets, "./dest/readme.md"); //Injects `snippets` into `./dest/readme.md`
//Returns snippets as `Record<string, string>`.
const snippets = await extractSnippets(searchOptions, "$start: ", "$end");
//Injects `snippets` into `./dest/readme.md` replacing all instances of `$snippet: snippet-name` with the appropriate snippet
await injectSnippetsIntoFile(snippets, "./dest/readme.md", "$snippet: ");
```

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc