@sytone/markdown-snippet-injector
Version:
The MarkDown snippet injector generates MD code snippets by extracting them from the source code of your projects.
87 lines • 3.61 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SourceFile = exports.InMemoryFileSystem = exports.NodeFileSystem = void 0;
/* eslint-disable import/extensions */
/* eslint-disable @typescript-eslint/consistent-type-imports */
const fs = __importStar(require("node:fs"));
const format_specs_1 = require("./format-specs");
const snippet_1 = require("./snippet");
class NodeFileSystem {
readFileSync(path, encoding) {
return fs.readFileSync(path, encoding);
}
}
exports.NodeFileSystem = NodeFileSystem;
class InMemoryFileSystem {
fileContents;
constructor(fileContents) {
this.fileContents = fileContents;
this.fileContents = fileContents;
}
readFileSync(path, encoding) {
return this.fileContents;
}
}
exports.InMemoryFileSystem = InMemoryFileSystem;
class SourceFile {
programOptions;
fileExtension;
file;
fs;
spec;
fileContents;
constructor(programOptions, fileExtension, file, fs = new NodeFileSystem()) {
this.programOptions = programOptions;
this.fileExtension = fileExtension;
this.file = file;
this.fs = fs;
this.spec = (0, format_specs_1.getFormatSpec)(fileExtension);
this.fileContents = this.fs.readFileSync(file, 'utf8');
}
// {comment} >> id='id' options='key=value&key=value' {/comment}
// {comment} >> the-name-of-the-snippet {/comment}
get openRegExp() {
return new RegExp(this.spec.commentStart + snippet_1.snippetStartPrefix.source + snippet_1.snippetToken.source + this.spec.commentEnd, 'g');
}
// {comment} >> the-name-of-the-snippet {/comment}
get openReplacerRegExp() {
return new RegExp(this.spec.commentStart + snippet_1.snippetStartPrefix.source + snippet_1.snippetToken.source + this.spec.commentEnd, 'g');
}
// {comment} << the-name-of-the-snippet {/comment}
get closeReplacerRegExp() {
return new RegExp(this.spec.commentStart + snippet_1.snippetEndPrefix.source + snippet_1.snippetName.source + this.spec.commentEnd, 'g');
}
// {comment} << the-name-of-the-snippet {/comment}
getClosingEofRegExp(id) {
return new RegExp(this.spec.commentStart + snippet_1.snippetEndPrefix.source + id + '$', 'm');
}
// {comment} << the-name-of-the-snippet([^-]){/comment}
getClosingRegExp(id) {
return new RegExp(this.spec.commentStart + snippet_1.snippetEndPrefix.source + id + '([^-])' + this.spec.commentEnd);
}
}
exports.SourceFile = SourceFile;
//# sourceMappingURL=source-file.js.map