@sytone/markdown-snippet-injector
Version:
The MarkDown snippet injector generates MD code snippets by extracting them from the source code of your projects.
78 lines (75 loc) • 2.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFormatSpec = exports.htmlSpec = exports.xmlSpec = exports.cssSpec = exports.jsSpec = void 0;
const whitespace = /[^\S\r\n]*/;
const wsAndLine = new RegExp(whitespace.source + '\\r?\\n');
/*
// >> id='snippet-injector-format-spec-js' options='file=format-spec/js.md'
title: Snippet Injector - Defining snippets in `JavaScript` and `TypeScript` source files
---
Defining code snippets in your source files is done by enclosing them with a starting token and
ending token prefixed by a line comment.
After the comment the two character `>>` indicate that there is a starting token. Next come
the `id` of the snippet, this is wrapped in single quotes like this `id='snippet-name-goes-here'`
next are options, if you are not setting any it should be empty like this `options=''`
// << snippet-injector-format-spec-js
*/
exports.jsSpec = {
commentStart: whitespace.source + '\\/\\/' + whitespace.source,
commentEnd: wsAndLine.source,
};
exports.cssSpec = {
commentStart: whitespace.source + '\\/\\*' + whitespace.source,
commentEnd: whitespace.source + '\\*\\/' + wsAndLine.source,
};
exports.xmlSpec = {
commentStart: whitespace.source + '<!--' + whitespace.source,
commentEnd: whitespace.source + '-->' + wsAndLine.source,
postProcess(snippet) {
const bindingRegEx = /{{.*}}/;
const newLineChar = '\n';
const linesOfSnippet = snippet.split(newLineChar);
let newSnippet = linesOfSnippet.length > 0 ? '' : snippet;
for (let i = 0; i < linesOfSnippet.length; i++) {
let currentLine = linesOfSnippet[i];
const match = bindingRegEx.exec(currentLine);
if (match) {
currentLine = '{% raw %}' + currentLine + '{% endraw %}';
}
newSnippet += currentLine;
if (i < linesOfSnippet.length - 1) {
newSnippet += newLineChar;
}
}
return newSnippet;
},
};
exports.htmlSpec = exports.xmlSpec;
function getFormatSpec(extension) {
switch (extension) {
case '.cs':
case '.swift':
case '.h':
case '.m':
case '.js':
case '.java':
case '.ts': {
return exports.jsSpec;
}
case '.css': {
return exports.cssSpec;
}
case '.xaml':
case '.xml': {
return exports.xmlSpec;
}
case '.html': {
return exports.htmlSpec;
}
default: {
throw new Error(`Unsupported file extension: ${extension}`);
}
}
}
exports.getFormatSpec = getFormatSpec;
//# sourceMappingURL=format-specs.js.map