md-slots 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +38 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +40 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Matt Holden
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# md-slots
|
|
2
|
+
|
|
3
|
+
Tiny `{{slot}}` renderer for markdown text.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install md-slots
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { renderSlots, slots } from "md-slots";
|
|
15
|
+
|
|
16
|
+
const template = "Hello {{ name }}.\n\n{{body}}";
|
|
17
|
+
|
|
18
|
+
slots(template);
|
|
19
|
+
// ["name", "body"]
|
|
20
|
+
|
|
21
|
+
renderSlots(template, {
|
|
22
|
+
name: "Ada",
|
|
23
|
+
body: "Welcome.",
|
|
24
|
+
});
|
|
25
|
+
// "Hello Ada.\n\nWelcome."
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## API
|
|
29
|
+
|
|
30
|
+
### `slots(template: string): string[]`
|
|
31
|
+
|
|
32
|
+
Returns unique slot names in order of first appearance. Slots look like `{{name}}`, with ignored whitespace inside the braces.
|
|
33
|
+
|
|
34
|
+
### `renderSlots(template, values, options?): string`
|
|
35
|
+
|
|
36
|
+
Replaces matching slots with `values[name]`. Strings render as-is; other values render with `JSON.stringify(value, null, 2)`. Pass `options.format(value, name)` to customize formatting.
|
|
37
|
+
|
|
38
|
+
Missing, `null`, and `undefined` values throw `Error("Missing slot: <name>")`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the unique slot names referenced by a template, preserving the order
|
|
3
|
+
* in which each name first appears.
|
|
4
|
+
*
|
|
5
|
+
* Slots must use `{{name}}` syntax, with optional whitespace inside the braces.
|
|
6
|
+
* Malformed placeholders are ignored.
|
|
7
|
+
*/
|
|
8
|
+
export declare function slots(template: string): string[];
|
|
9
|
+
/**
|
|
10
|
+
* Replaces each matching slot in a template with its corresponding value.
|
|
11
|
+
*
|
|
12
|
+
* String values are rendered as-is. Other present values are rendered as
|
|
13
|
+
* pretty-printed JSON unless `options.format` is provided. Missing, `null`, and
|
|
14
|
+
* `undefined` values throw a `Missing slot` error.
|
|
15
|
+
*/
|
|
16
|
+
export declare function renderSlots(template: string, values: Record<string, unknown>, options?: {
|
|
17
|
+
format?: (value: unknown, name: string) => string;
|
|
18
|
+
}): string;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const slotPattern = /\{\{\s*([A-Za-z][A-Za-z0-9_-]*)\s*\}\}/g;
|
|
2
|
+
/**
|
|
3
|
+
* Returns the unique slot names referenced by a template, preserving the order
|
|
4
|
+
* in which each name first appears.
|
|
5
|
+
*
|
|
6
|
+
* Slots must use `{{name}}` syntax, with optional whitespace inside the braces.
|
|
7
|
+
* Malformed placeholders are ignored.
|
|
8
|
+
*/
|
|
9
|
+
export function slots(template) {
|
|
10
|
+
const names = new Set();
|
|
11
|
+
for (const match of template.matchAll(slotPattern)) {
|
|
12
|
+
names.add(match[1]);
|
|
13
|
+
}
|
|
14
|
+
return [...names];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Replaces each matching slot in a template with its corresponding value.
|
|
18
|
+
*
|
|
19
|
+
* String values are rendered as-is. Other present values are rendered as
|
|
20
|
+
* pretty-printed JSON unless `options.format` is provided. Missing, `null`, and
|
|
21
|
+
* `undefined` values throw a `Missing slot` error.
|
|
22
|
+
*/
|
|
23
|
+
export function renderSlots(template, values, options) {
|
|
24
|
+
return template.replace(slotPattern, (_match, name) => {
|
|
25
|
+
if (!Object.hasOwn(values, name)) {
|
|
26
|
+
throw new Error(`Missing slot: ${name}`);
|
|
27
|
+
}
|
|
28
|
+
const value = values[name];
|
|
29
|
+
if (value === null || value === undefined) {
|
|
30
|
+
throw new Error(`Missing slot: ${name}`);
|
|
31
|
+
}
|
|
32
|
+
if (options?.format) {
|
|
33
|
+
return options.format(value, name);
|
|
34
|
+
}
|
|
35
|
+
if (typeof value === "string") {
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
return JSON.stringify(value, null, 2);
|
|
39
|
+
});
|
|
40
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "md-slots",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Tiny {{slot}} renderer for markdown text.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/holdenmatt/md-slots.git"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"test": "vitest run",
|
|
23
|
+
"lint": "oxlint",
|
|
24
|
+
"format": "oxfmt --write .",
|
|
25
|
+
"format:check": "oxfmt --check .",
|
|
26
|
+
"check": "pnpm format:check && pnpm lint && pnpm test && pnpm build",
|
|
27
|
+
"prepare": "tsc"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^26.0.1",
|
|
31
|
+
"oxfmt": "^0.56.0",
|
|
32
|
+
"oxlint": "^1.71.0",
|
|
33
|
+
"typescript": "^6.0.3",
|
|
34
|
+
"vitest": "^4.1.9"
|
|
35
|
+
},
|
|
36
|
+
"packageManager": "pnpm@10.33.0"
|
|
37
|
+
}
|