@rspress-theme-anatole/plugin-container-syntax 0.0.2

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/container.css ADDED
@@ -0,0 +1,206 @@
1
+ /*
2
+ * Container Style
3
+ */
4
+ :root {
5
+ --rp-container-note-border: var(--rp-c-divider-light);
6
+ --rp-container-note-text: var(--rp-c-text-1);
7
+ --rp-container-note-bg: var(--rp-c-bg-soft);
8
+ --rp-container-note-code-bg: rgba(128, 128, 128, 0.1);
9
+ --rp-container-note-link: var(--rp-c-link);
10
+
11
+ --rp-container-tip-border: rgba(7, 156, 112, 0.2);
12
+ --rp-container-tip-text: #278359;
13
+ --rp-container-tip-bg: rgba(7, 156, 112, 0.06);
14
+ --rp-container-tip-code-bg: rgba(7, 156, 112, 0.1);
15
+
16
+ --rp-container-info-border: rgba(0, 149, 255, 0.2);
17
+ --rp-container-info-text: #07f;
18
+ --rp-container-info-bg: rgba(0, 149, 255, 0.06);
19
+ --rp-container-info-code-bg: rgba(0, 149, 255, 0.1);
20
+
21
+ --rp-container-warning-border: rgba(255, 197, 23, 0.4);
22
+ --rp-container-warning-text: #887233;
23
+ --rp-container-warning-bg: rgba(255, 197, 23, 0.1);
24
+ --rp-container-warning-code-bg: rgba(255, 197, 23, 0.1);
25
+
26
+ --rp-container-danger-border: rgba(237, 60, 80, 0.2);
27
+ --rp-container-danger-text: #ab2131;
28
+ --rp-container-danger-bg: rgba(237, 60, 80, 0.08);
29
+ --rp-container-danger-code-bg: rgba(237, 60, 80, 0.1);
30
+
31
+ --rp-container-details-border: var(--rp-c-divider-light);
32
+ --rp-container-details-text: var(--rp-c-text-1);
33
+ --rp-container-details-bg: var(--rp-c-bg-soft);
34
+ --rp-container-details-code-bg: rgba(128, 128, 128, 0.1);
35
+ --rp-container-details-link: var(--rp-c-link);
36
+ }
37
+
38
+ .dark {
39
+ --rp-container-tip-text: #3ec480;
40
+ --rp-container-tip-bg: rgba(7, 156, 112, 0.1);
41
+
42
+ --rp-container-info-text: #66c2ff;
43
+ --rp-container-info-bg: rgba(0, 149, 255, 0.1);
44
+
45
+ --rp-container-warning-text: rgb(251, 180, 81);
46
+ --rp-container-warning-border: rgba(255, 197, 23, 0.25);
47
+ --rp-container-warning-bg: rgba(255, 197, 23, 0.12);
48
+
49
+ --rp-container-danger-text: rgb(247, 110, 133);
50
+ --rp-container-danger-border: rgba(237, 60, 80, 0.3);
51
+ --rp-container-danger-bg: rgba(237, 60, 80, 0.12);
52
+ }
53
+
54
+ .rspress-doc .rspress-directive {
55
+ border: 1px solid transparent;
56
+ border-radius: var(--rp-radius);
57
+ padding: 20px 24px 12px;
58
+ line-height: 1.7;
59
+ font-size: 14px;
60
+ font-weight: 400;
61
+ margin: 24px 0;
62
+ }
63
+
64
+ .rspress-doc .rspress-directive .rspress-directive-title {
65
+ margin-bottom: 8px;
66
+ font-weight: 600;
67
+ font-size: 16px;
68
+ }
69
+
70
+ .rspress-doc .rspress-directive .rspress-directive-content p {
71
+ margin: 8px 0;
72
+ }
73
+
74
+ .rspress-doc .rspress-directive .rspress-directive-icon {
75
+ display: inline-block;
76
+ width: 20px;
77
+ height: 20px;
78
+ margin-right: 3px;
79
+ }
80
+
81
+ .rspress-doc .rspress-directive p {
82
+ margin: 8px 0;
83
+ }
84
+
85
+ .rspress-doc .rspress-directive code {
86
+ color: inherit;
87
+ font-size: 13px;
88
+ }
89
+
90
+ .rspress-doc .rspress-directive a {
91
+ font-weight: 500;
92
+ transition: color 0.25s;
93
+ border-bottom: 1px solid currentColor;
94
+ }
95
+
96
+ .rspress-doc .rspress-directive.note {
97
+ border-color: var(--rp-container-note-border);
98
+ background-color: var(--rp-container-note-bg);
99
+ }
100
+
101
+ .rspress-doc .rspress-directive.note .rspress-directive-title {
102
+ color: var(--rp-container-note-text);
103
+ }
104
+
105
+ .rspress-doc .rspress-directive.note code {
106
+ color: var(--rp-container-note-text);
107
+ background-color: var(--rp-container-note-code-bg);
108
+ }
109
+
110
+ .rspress-doc .rspress-directive.note a {
111
+ color: var(--rp-container-note-text);
112
+ }
113
+
114
+ .rspress-doc .rspress-directive.tip {
115
+ border-color: var(--rp-container-tip-border);
116
+ background-color: var(--rp-container-tip-bg);
117
+ }
118
+
119
+ .rspress-doc .rspress-directive.tip .rspress-directive-title {
120
+ color: var(--rp-container-tip-text);
121
+ }
122
+
123
+ .rspress-doc .rspress-directive.tip code {
124
+ color: var(--rp-container-tip-text);
125
+ background-color: var(--rp-container-tip-code-bg);
126
+ }
127
+
128
+ .rspress-doc .rspress-directive.tip a {
129
+ color: var(--rp-container-tip-text);
130
+ }
131
+
132
+ .rspress-doc .rspress-directive.info {
133
+ border-color: var(--rp-container-info-border);
134
+ background-color: var(--rp-container-info-bg);
135
+ }
136
+
137
+ .rspress-doc .rspress-directive.info .rspress-directive-title {
138
+ color: var(--rp-container-info-text);
139
+ }
140
+
141
+ .rspress-doc .rspress-directive.info code {
142
+ color: var(--rp-container-info-text);
143
+ background-color: var(--rp-container-info-code-bg);
144
+ }
145
+
146
+ .rspress-doc .rspress-directive.info a {
147
+ color: var(--rp-container-info-text);
148
+ }
149
+
150
+ .rspress-doc .rspress-directive.warning {
151
+ border-color: var(--rp-container-warning-border);
152
+ background-color: var(--rp-container-warning-bg);
153
+ }
154
+
155
+ .rspress-doc .rspress-directive.warning .rspress-directive-title {
156
+ color: var(--rp-container-warning-text);
157
+ }
158
+
159
+ .rspress-doc .rspress-directive.warning code {
160
+ color: var(--rp-container-warning-text);
161
+ background-color: var(--rp-container-warning-code-bg);
162
+ }
163
+
164
+ .rspress-doc .rspress-directive.warning a {
165
+ color: var(--rp-container-warning-text);
166
+ }
167
+
168
+ .rspress-doc .rspress-directive.caution,
169
+ .rspress-doc .rspress-directive.danger {
170
+ border-color: var(--rp-container-danger-border);
171
+ background-color: var(--rp-container-danger-bg);
172
+ }
173
+
174
+ .rspress-doc .rspress-directive.caution .rspress-directive-title,
175
+ .rspress-doc .rspress-directive.danger .rspress-directive-title {
176
+ color: var(--rp-container-danger-text);
177
+ }
178
+
179
+ .rspress-doc .rspress-directive.caution code,
180
+ .rspress-doc .rspress-directive.danger code {
181
+ color: var(--rp-container-danger-text);
182
+ background-color: var(--rp-container-danger-code-bg);
183
+ }
184
+
185
+ .rspress-doc .rspress-directive.caution a,
186
+ .rspress-doc .rspress-directive.danger a {
187
+ color: var(--rp-container-danger-text);
188
+ }
189
+
190
+ .rspress-doc .rspress-directive.details {
191
+ border-color: var(--rp-container-details-border);
192
+ background-color: var(--rp-container-details-bg);
193
+ }
194
+
195
+ .rspress-doc .rspress-directive.details .rspress-directive-title {
196
+ color: var(--rp-container-details-text);
197
+ }
198
+
199
+ .rspress-doc .rspress-directive.details code {
200
+ color: var(--rp-container-details-text);
201
+ background-color: var(--rp-container-details-code-bg);
202
+ }
203
+
204
+ .rspress-doc .rspress-directive.details a {
205
+ color: var(--rp-container-details-link);
206
+ }
@@ -0,0 +1,5 @@
1
+ import { RspressPlugin } from '@rspress-theme-anatole/shared';
2
+
3
+ export declare function pluginContainerSyntax(): RspressPlugin;
4
+
5
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,212 @@
1
+ import * as __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__ from "node:path";
2
+ import * as __WEBPACK_EXTERNAL_MODULE_node_url_e96de089__ from "node:url";
3
+ import * as __WEBPACK_EXTERNAL_MODULE__rspress_shared_baa012d0__ from "@rspress-theme-anatole/shared";
4
+ const DIRECTIVE_TYPES = [
5
+ 'tip',
6
+ 'note',
7
+ 'warning',
8
+ 'caution',
9
+ 'danger',
10
+ 'info',
11
+ 'details'
12
+ ];
13
+ const REGEX_BEGIN = /^\s*:::\s*(\w+)\s*(.*)?/;
14
+ const REGEX_END = /\s*:::$/;
15
+ const REGEX_GH_BEGIN = /^\s*\s*\[!(\w+)\]\s*(.*)?/;
16
+ const TITLE_REGEX_IN_MD = /{\s*title=["']?(.+)}\s*/;
17
+ const TITLE_REGEX_IN_MDX = /\s*title=["']?(.+)\s*/;
18
+ const trimTailingQuote = (str)=>str.replace(/['"]$/g, '');
19
+ const parseTitle = (rawTitle = '', isMDX = false)=>{
20
+ const matched = rawTitle?.match(isMDX ? TITLE_REGEX_IN_MDX : TITLE_REGEX_IN_MD);
21
+ return trimTailingQuote(matched?.[1] || rawTitle);
22
+ };
23
+ const createContainer = (type, title, children)=>{
24
+ const isDetails = 'details' === type;
25
+ const rootHName = isDetails ? 'details' : 'div';
26
+ const titleHName = isDetails ? 'summary' : 'div';
27
+ return {
28
+ type: 'containerDirective',
29
+ data: {
30
+ hName: rootHName,
31
+ hProperties: {
32
+ class: `rspress-directive ${type}`
33
+ }
34
+ },
35
+ children: [
36
+ {
37
+ type: 'paragraph',
38
+ data: {
39
+ hName: titleHName,
40
+ hProperties: {
41
+ class: 'rspress-directive-title'
42
+ }
43
+ },
44
+ children: [
45
+ {
46
+ type: 'text',
47
+ value: title || type.toUpperCase()
48
+ }
49
+ ]
50
+ },
51
+ {
52
+ type: 'paragraph',
53
+ data: {
54
+ hName: 'div',
55
+ hProperties: {
56
+ class: 'rspress-directive-content'
57
+ }
58
+ },
59
+ children: children
60
+ }
61
+ ]
62
+ };
63
+ };
64
+ function transformer(tree) {
65
+ let i = 0;
66
+ try {
67
+ while(i < tree.children.length){
68
+ const node = tree.children[i];
69
+ if ('children' in node) transformer(node);
70
+ if ('containerDirective' === node.type) {
71
+ const type = node.name;
72
+ if (DIRECTIVE_TYPES.includes(type)) tree.children.splice(i, 1, createContainer(type, node.attributes?.title ?? type.toUpperCase(), node.children));
73
+ } else if ('blockquote' === node.type && 'paragraph' === node.children[0].type && 'value' in node.children[0].children[0]) {
74
+ const initiatorTag = node.children[0].children[0].value;
75
+ const match = initiatorTag.match(REGEX_GH_BEGIN);
76
+ if (match) {
77
+ const [, type] = match;
78
+ if (!DIRECTIVE_TYPES.includes(type.toLowerCase())) {
79
+ i++;
80
+ continue;
81
+ }
82
+ if (1 === node.children.length && 'paragraph' === node.children[0].type) node.children[0].children[0].value = match[2] ?? '';
83
+ const newChild = createContainer(type.toLowerCase(), type.toUpperCase(), 0 === node.children.slice(1).length ? node.children.slice(0) : node.children.slice(1));
84
+ tree.children.splice(i, 1, newChild);
85
+ }
86
+ }
87
+ if ('paragraph' !== node.type || 'text' !== node.children[0].type) {
88
+ i++;
89
+ continue;
90
+ }
91
+ const firstTextNode = node.children[0];
92
+ const text = firstTextNode.value;
93
+ const metaText = text.split('\n')[0];
94
+ const content = text.slice(metaText.length);
95
+ const match = metaText.match(REGEX_BEGIN);
96
+ if (!match) {
97
+ i++;
98
+ continue;
99
+ }
100
+ const [, type, rawTitle] = match;
101
+ let title = parseTitle(rawTitle);
102
+ const titleExpressionNode = node.children[1] && 'mdxTextExpression' === node.children[1].type ? node.children[1] : null;
103
+ if (titleExpressionNode) {
104
+ title = parseTitle(titleExpressionNode.value, true);
105
+ node.children.splice(1, 1);
106
+ }
107
+ if (!DIRECTIVE_TYPES.includes(type)) {
108
+ i++;
109
+ continue;
110
+ }
111
+ const wrappedChildren = [];
112
+ if (content?.endsWith(':::')) {
113
+ wrappedChildren.push({
114
+ type: 'paragraph',
115
+ children: [
116
+ {
117
+ type: 'text',
118
+ value: content.replace(REGEX_END, '')
119
+ }
120
+ ]
121
+ });
122
+ const newChild = createContainer(type, title, wrappedChildren);
123
+ tree.children.splice(i, 1, newChild);
124
+ } else {
125
+ const paragraphChild = {
126
+ type: 'paragraph',
127
+ children: []
128
+ };
129
+ wrappedChildren.push(paragraphChild);
130
+ if (content.length) paragraphChild.children.push({
131
+ type: 'text',
132
+ value: content
133
+ });
134
+ paragraphChild.children.push(...node.children.slice(1, -1));
135
+ if (0 === paragraphChild.children.length) wrappedChildren.pop();
136
+ const lastChildInNode = node.children[node.children.length - 1];
137
+ if ('text' === lastChildInNode.type && REGEX_END.test(lastChildInNode.value)) {
138
+ const lastChildInNodeText = lastChildInNode.value;
139
+ const matchedEndContent = lastChildInNodeText.slice(0, -3).trim();
140
+ if (wrappedChildren.length) wrappedChildren[0].children.push({
141
+ type: 'text',
142
+ value: matchedEndContent
143
+ });
144
+ else if (matchedEndContent) wrappedChildren.push({
145
+ type: 'paragraph',
146
+ children: [
147
+ {
148
+ type: 'text',
149
+ value: matchedEndContent
150
+ }
151
+ ]
152
+ });
153
+ const newChild = createContainer(type, title, wrappedChildren);
154
+ tree.children.splice(i, 1, newChild);
155
+ i++;
156
+ continue;
157
+ }
158
+ if (lastChildInNode !== firstTextNode && wrappedChildren.length) wrappedChildren[0].children.push(lastChildInNode);
159
+ let j = i + 1;
160
+ while(j < tree.children.length){
161
+ const currentParagraph = tree.children[j];
162
+ if ('paragraph' !== currentParagraph.type) {
163
+ wrappedChildren.push(currentParagraph);
164
+ j++;
165
+ continue;
166
+ }
167
+ const lastChild = currentParagraph.children[currentParagraph.children.length - 1];
168
+ if (lastChild === firstTextNode || 'text' === lastChild.type && REGEX_END.test(lastChild.value)) {
169
+ const lastChildText = lastChild.value;
170
+ const matchedEndContent = lastChildText.slice(0, -3).trim();
171
+ wrappedChildren.push(...currentParagraph.children.filter((child)=>child !== firstTextNode && child !== lastChild));
172
+ if (matchedEndContent) wrappedChildren.push({
173
+ type: 'paragraph',
174
+ children: [
175
+ {
176
+ type: 'text',
177
+ value: matchedEndContent
178
+ }
179
+ ]
180
+ });
181
+ const newChild = createContainer(type, title, wrappedChildren);
182
+ tree.children.splice(i, j - i + 1, newChild);
183
+ break;
184
+ }
185
+ wrappedChildren.push({
186
+ ...currentParagraph,
187
+ children: currentParagraph.children.filter((child)=>child !== firstTextNode)
188
+ });
189
+ j++;
190
+ }
191
+ }
192
+ i++;
193
+ }
194
+ } catch (e) {
195
+ console.log(e);
196
+ throw e;
197
+ }
198
+ }
199
+ const remarkPluginContainer = ()=>transformer;
200
+ const src_dirname = (0, __WEBPACK_EXTERNAL_MODULE__rspress_shared_baa012d0__.normalizePosixPath)(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].dirname((0, __WEBPACK_EXTERNAL_MODULE_node_url_e96de089__.fileURLToPath)(import.meta.url)));
201
+ function pluginContainerSyntax() {
202
+ return {
203
+ name: '@rspress-theme-anatole/plugin-container-syntax',
204
+ markdown: {
205
+ remarkPlugins: [
206
+ remarkPluginContainer
207
+ ]
208
+ },
209
+ globalStyles: __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].posix.join(src_dirname, '../container.css')
210
+ };
211
+ }
212
+ export { pluginContainerSyntax };
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@rspress-theme-anatole/plugin-container-syntax",
3
+ "version": "0.0.2",
4
+ "license": "MIT",
5
+ "sideEffects": [
6
+ "*.css",
7
+ "*.less",
8
+ "*.sass",
9
+ "*.scss"
10
+ ],
11
+ "type": "module",
12
+ "main": "./dist/index.js",
13
+ "module": "./dist/index.mjs",
14
+ "types": "./dist/index.d.ts",
15
+ "files": [
16
+ "dist",
17
+ "container.css"
18
+ ],
19
+ "dependencies": {
20
+ "@rspress-theme-anatole/shared": "0.1.19"
21
+ },
22
+ "devDependencies": {
23
+ "@microsoft/api-extractor": "^7.49.2",
24
+ "@rslib/core": "0.5.2",
25
+ "@types/mdast": "^3.0.15",
26
+ "mdast-util-mdx-expression": "^1.3.2",
27
+ "mdast-util-mdx-jsx": "^2.1.4",
28
+ "rehype-stringify": "^9.0.4",
29
+ "remark-directive": "^2.0.1",
30
+ "remark-mdx": "2.3.0",
31
+ "remark-parse": "^10.0.2",
32
+ "remark-rehype": "^10.1.0",
33
+ "unified": "^10.1.2"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public"
37
+ }
38
+ }