nizel-plugin-alert 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/README.md +69 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +52 -0
- package/dist/index.js.map +1 -0
- package/package.json +31 -0
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# nizel-plugin-alert
|
|
2
|
+
|
|
3
|
+
GitHub-style alert blocks for [Nizel](https://npmjs.com/package/nizel).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install nizel-plugin-alert
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { useNizel } from 'nizel';
|
|
15
|
+
import { alertPlugin } from 'nizel-plugin-alert';
|
|
16
|
+
|
|
17
|
+
const nizel = useNizel({
|
|
18
|
+
plugins: [alertPlugin()],
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const html = await nizel.html(`::warning Careful
|
|
22
|
+
Pay **attention**.
|
|
23
|
+
::`);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Syntax
|
|
27
|
+
|
|
28
|
+
The plugin registers these custom block names:
|
|
29
|
+
|
|
30
|
+
- `note`
|
|
31
|
+
- `tip`
|
|
32
|
+
- `important`
|
|
33
|
+
- `warning`
|
|
34
|
+
- `caution`
|
|
35
|
+
|
|
36
|
+
The first argument or `title` prop is rendered as the alert title.
|
|
37
|
+
|
|
38
|
+
```md
|
|
39
|
+
::tip Helpful
|
|
40
|
+
Use Markdown inside the alert body.
|
|
41
|
+
::
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```md
|
|
45
|
+
::tip
|
|
46
|
+
title: Helpful
|
|
47
|
+
::
|
|
48
|
+
Use Markdown inside the alert body.
|
|
49
|
+
::
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Options
|
|
53
|
+
|
|
54
|
+
| Option | Type | Default | Description |
|
|
55
|
+
| --- | --- | --- | --- |
|
|
56
|
+
| `className` | `string` | `'alert'` | Root CSS class used for generated alert markup. |
|
|
57
|
+
|
|
58
|
+
## Output
|
|
59
|
+
|
|
60
|
+
```html
|
|
61
|
+
<div class="alert alert--warning" data-alert="warning">
|
|
62
|
+
<p class="alert__title">Careful</p>
|
|
63
|
+
<div class="alert__content">...</div>
|
|
64
|
+
</div>
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type NizelPlugin } from 'nizel';
|
|
2
|
+
export type AlertType = 'note' | 'tip' | 'important' | 'warning' | 'caution';
|
|
3
|
+
export type AlertPluginOptions = {
|
|
4
|
+
className?: string;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Creates a GitHub-style alert block plugin.
|
|
8
|
+
*/
|
|
9
|
+
export declare const alertPlugin: (options?: AlertPluginOptions) => NizelPlugin;
|
|
10
|
+
export default alertPlugin;
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,WAAW,EAEjB,MAAM,OAAO,CAAC;AAEf,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,KAAK,GAAG,WAAW,GAAG,SAAS,GAAG,SAAS,CAAC;AAE7E,MAAM,MAAM,kBAAkB,GAAG;IAC/B,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AASF;;GAEG;AACH,eAAO,MAAM,WAAW,GAAI,UAAS,kBAAuB,KAAG,WAS9D,CAAC;AAkDF,eAAe,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { defineNizelPlugin, } from 'nizel';
|
|
2
|
+
const ALERT_TYPES = ['note', 'tip', 'important', 'warning', 'caution'];
|
|
3
|
+
/**
|
|
4
|
+
* Creates a GitHub-style alert block plugin.
|
|
5
|
+
*/
|
|
6
|
+
export const alertPlugin = (options = {}) => {
|
|
7
|
+
const blocks = Object.fromEntries(ALERT_TYPES.map((alertType) => [alertType, createAlertBlock(alertType, options)]));
|
|
8
|
+
return defineNizelPlugin({
|
|
9
|
+
name: 'alert',
|
|
10
|
+
blocks,
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Creates a custom block definition for one alert type.
|
|
15
|
+
*/
|
|
16
|
+
const createAlertBlock = (alertType, options) => {
|
|
17
|
+
return {
|
|
18
|
+
name: alertType,
|
|
19
|
+
parse({ args, props }) {
|
|
20
|
+
const title = props.title ?? args[0] ?? '';
|
|
21
|
+
return { alertType, title: String(title) };
|
|
22
|
+
},
|
|
23
|
+
formats: {
|
|
24
|
+
html(node, ctx) {
|
|
25
|
+
if (node.type !== 'customBlock' || !isAlertValue(node.value))
|
|
26
|
+
return '';
|
|
27
|
+
return renderAlert(node.value, node.children ?? [], ctx, options);
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Renders an alert block with escaped attributes and title text.
|
|
34
|
+
*/
|
|
35
|
+
const renderAlert = (value, children, ctx, options) => {
|
|
36
|
+
const rootClass = options.className ?? 'alert';
|
|
37
|
+
const titleHtml = value.title ? `<p class="${rootClass}__title">${ctx.escape(value.title)}</p>` : '';
|
|
38
|
+
const contentHtml = ctx.render(children);
|
|
39
|
+
const escapedType = ctx.escape(value.alertType);
|
|
40
|
+
return `<div class="${ctx.escape(rootClass)} ${ctx.escape(rootClass)}--${escapedType}" data-alert="${escapedType}">${titleHtml}<div class="${ctx.escape(rootClass)}__content">${contentHtml}</div></div>`;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Checks that a custom block value was produced by the alert parser.
|
|
44
|
+
*/
|
|
45
|
+
const isAlertValue = (value) => {
|
|
46
|
+
if (!value || typeof value !== 'object')
|
|
47
|
+
return false;
|
|
48
|
+
const candidate = value;
|
|
49
|
+
return typeof candidate.title === 'string' && ALERT_TYPES.includes(candidate.alertType);
|
|
50
|
+
};
|
|
51
|
+
export default alertPlugin;
|
|
52
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,GAIlB,MAAM,OAAO,CAAC;AAaf,MAAM,WAAW,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,CAAU,CAAC;AAEhF;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,UAA8B,EAAE,EAAe,EAAE;IAC3E,MAAM,MAAM,GAAG,MAAM,CAAC,WAAW,CAC/B,WAAW,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,SAAS,EAAE,gBAAgB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC,CACvC,CAAC;IAE7C,OAAO,iBAAiB,CAAC;QACvB,IAAI,EAAE,OAAO;QACb,MAAM;KACP,CAAC,CAAC;AACL,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,gBAAgB,GAAG,CACvB,SAAoB,EACpB,OAA2B,EACL,EAAE;IACxB,OAAO;QACL,IAAI,EAAE,SAAS;QACf,KAAK,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE;YACnB,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;YAC3C,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;QAC7C,CA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AC,KAAK,KAAK,QAAQ,IAAI,WAAW,CAAC,QAAQ,CAAC,SAAS,CAAC,SAAsB,CAAC,CAAC;AACvG,CAAC,CAAC;AAEF,eAAe,WAAW,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nizel-plugin-alert",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "GitHub-style alert blocks plugin for Nizel.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc -p tsconfig.json",
|
|
20
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
21
|
+
"test": "npm run build && vitest run test/*.mjs",
|
|
22
|
+
"test:watch": "npm run build && vitest test/*.mjs"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"nizel": "^0.1.0"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public",
|
|
29
|
+
"provenance": true
|
|
30
|
+
}
|
|
31
|
+
}
|