Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
markdown-snippet-injector
Advanced tools
The MarkDown snippet injector generates MD code snippets by extracting them from the source code of your projects.
markdown-snippet-injector
A tool for injecting code snippets into MarkDown files:
mdinject --root=<path-to-source-code> --docsroot=<path-to-docs>
markdown-snippet-injector
JavaScript
and TypeScript
source filesDefining code snippets in your source files is done by enclosing them as shown below:
// >> sum-snippet
export function sum(a, b){
return a + b;
}
// << sum-snippet
// >> multiplication-snippet
export function multiply(a, b){
return a * b;
}
// << multiplication-snippet
// >> division-snippet
export function divide(a, b){
return a / b;
}
// << division-snippet
XML
filesIf you want to define a code-snippet in a XML
file you should use the following approach:
<!-- >> listview-first-look -->
<navigation:ExamplePage xmlns:navigation="navigation/example-page" loaded="onPageLoaded" xmlns:lv="nativescript-telerik-ui/listview" xmlns="http://www.nativescript.org/tns.xsd">
<lv:RadListView items="{{ dataItems }}" >
<lv:RadListView.listViewLayout>
<lv:ListViewLinearLayout scrollDirection="Vertical"/>
</lv:RadListView.listViewLayout>
<lv:RadListView.itemTemplate>
<StackLayout orientation="vertical">
<Label fontSize="20" text="{{ itemName }}"/>
<Label fontSize="14" text="{{ itemDescription }}"/>
</StackLayout>
</lv:RadListView.itemTemplate>
</lv:RadListView>
</navigation:ExamplePage>
<!-- << listview-first-look -->
CSS
filesCode snippets inside CSS files are defined as follows:
/* >> css-snippet */
.btn {
color: green;
text-align: center;
}
/* << css-snippet */
MarkDown
filesUse the <snippet id='<your-snippet-id>'/>
notation to define the corresponding placeholders in your markdown files. They will be replaced by the snippet injector when run:
# Using the multiply function:
<snippet id='multiplication-snippet'/>
# Using the sum function:
<snippet id='sum-snippet'/>
If you set -w
flag 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.
Example:
mdinject -w --root=<path-to-source-code> --docsroot=<path-to-docs>
main.css
/* >> css-snippet */
.btn {
color: green;
text-align: center;
}
/* << css-snippet */
README.MD
This is a CSS snippet
<snippet id='css-snippet'/>
After first build the README.MD will looks like:
This is a CSS snippet
<snippet id='css-snippet'>
.btn { color: green; text-align: center; }
</snippet>
Then when you update main.css
, your README.MD will be updated as well.
Nested snippets are also supported. This is helpful in scenarios where you want to explain parts of a larger snippet in steps:
// >> view-model-snippet
export class ViewModel {
private _items: ObservableArray<DataItem>;
constructor() {
this.initDataItems();
}
get dataItems() {
return this._items;
}
// >> handling-event-snippet
public onShouldRefreshOnPull(args: listViewModule.ListViewEventData) {
var that = new WeakRef(this);
console.log("Event fired");
timer.setTimeout(function() {
for (var i = 0; i < 25; i++) {
that.get()._items.splice(0, 0, new DataItem(that.get()._items.length, "Item " + that.get()._items.length, "This is item description."));
}
var listView = args.object;
listView.notifyPullToRefreshFinished();
}, 1000);
}
// << handling-event-snippet
private initDataItems() {
this._items = new ObservableArray<DataItem>();
for (var i = 0; i < 25; i++) {
this._items.push(new DataItem(i, "Item " + i, "This is item description."));
}
}
}
export class DataItem {
public id: number;
public itemName;
public itemDescription;
constructor(id: number, name: string, description: string) {
this.id = id;
this.itemName = name;
this.itemDescription = description;
}
}
// << view-model-snippet
This will produce two code snippets: one containing the whole view-model class and the other containing the onShouldRefreshOnPull
function.
You can mark parts of the original code to be hidden - not shown in the documentation:
// >> ts-snippet-with-hidden-section
export function div(a, b){
// >> (hide)
console.log("You should not see this!")
// << (hide)
return a / b;
}
// << ts-snippet-with-hidden-section
The syntax is similar in XML
and CSS
.
You can choose what kind of files will be processed during snippet injection by using the --sourceext
and --targetext
parameters. The default values of these properties are .ts
and .md
respectively.
You can define multiple source or target extension types by setting the corresponding parameters to a set of extensions separated by a |
:
mdinject --root=. --docsroot=../ --sourceext=".ts|.js" --targetext=".md|.txt"
In this way all target files will be processed and the corresponding snippet placeholders will be replaced.
When injected, a snippet is formatted using the default MarkDown code-snippet format. You can append a title to the injected snippet by using the --snippettitles
parameter. By default, .js
and .ts
files are recognized and the snippets coming from them are titled JavaScript
or TypeScript
. You can define custom snippet titles by setting the --snippettitles
parameter to a set of titles separated by a |
:
mdinject --root=. --docsroot=../ --sourceext=".java|.cs" --targetext=".md|.txt" --snippettitles="Java|C#"
Note that the order of the snippet titles must be the related to the order of the source extension types so that they match.
E2E tests are developed with Mocha.
FAQs
The MarkDown snippet injector generates MD code snippets by extracting them from the source code of your projects.
The npm package markdown-snippet-injector receives a total of 3 weekly downloads. As such, markdown-snippet-injector popularity was classified as not popular.
We found that markdown-snippet-injector demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.