@anthonypena/marked-toc-for-series 1.0.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/.eslintrc.cjs ADDED
@@ -0,0 +1,4 @@
1
+ module.exports = {
2
+ plugins: ['@anthonypena'],
3
+ extends: ['plugin:@anthonypena/recommended'],
4
+ };
@@ -0,0 +1,2 @@
1
+ dist/
2
+ node_modules/
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Anthony Pena
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,77 @@
1
+ # @anthonypena/marked-toc-for-series
2
+
3
+ ## Install
4
+
5
+ ```Bash
6
+ npm i @anthonypena/marked-toc-for-series
7
+ ```
8
+
9
+ ## Examples
10
+
11
+ ### Add the plugin to your Marked instance
12
+
13
+ ```TypeScript
14
+ import { markedDjotDiv } from '@anthonypena/marked-toc-for-series';
15
+ import { Marked } from 'marked';
16
+
17
+ const marked = new Marked();
18
+
19
+ marked.use(markedTocForSeries({
20
+ title: 'Série',
21
+ series: {
22
+ 'this-is-the-serie-id': {
23
+ articles: [
24
+ { label: 'First one', url: 'https://example.com/first-one' },
25
+ { label: 'Second one', url: '/second-one' },
26
+ ]
27
+ },
28
+ 'another-serie': {
29
+ // ...
30
+ }
31
+ }
32
+ }));
33
+ ```
34
+
35
+ ## Transformation
36
+
37
+ From:
38
+
39
+ ```Markdown
40
+ # Title
41
+
42
+ <!-- TOC-SERIE:this-is-the-serie-id -->
43
+
44
+ Some content...
45
+ ```
46
+
47
+ to:
48
+
49
+ ```Html
50
+ <h1>Title</h1>
51
+
52
+ <nav data-serie="this-is-the-serie-id">
53
+ <h2>Série</h2>
54
+ <ul>
55
+ <li><a href="https://example.com/first-one">First one</a></li>
56
+ <li><a href="/second-one">Second one</a></li>
57
+ </ul>
58
+ </nav>
59
+
60
+ <p>Some content...</p>
61
+ ```
62
+
63
+ > Note: This package does not provide build in style. You need to implement yourself style associated to classes you define.
64
+
65
+ ## Docs
66
+
67
+ ### Parameters
68
+
69
+ #### `title` (optional): the title you wan
70
+
71
+ Default: undefined
72
+
73
+ If defined, a title with an `<h2>` tag will be added, if not defined (undefined or null or empty) no title will be present.
74
+
75
+ #### `series`: the series definition
76
+
77
+ A dictionnary of serie definition.
@@ -0,0 +1,25 @@
1
+ import { marked } from 'marked';
2
+
3
+ type SeriesDef = Record<string, SerieDef>;
4
+ interface MarkedTocForSeriesOptions {
5
+ title?: string | undefined | null;
6
+ series: SeriesDef;
7
+ }
8
+ interface SerieDef {
9
+ articles: ArticleDef[];
10
+ }
11
+ interface ArticleDef {
12
+ label: string;
13
+ url: string;
14
+ }
15
+ declare function markedTocForSeries({ title, series, }: MarkedTocForSeriesOptions): marked.MarkedExtension;
16
+ interface TocForSeriesToken extends marked.Tokens.Generic {
17
+ type: 'tocForSeries';
18
+ serieId: string;
19
+ raw: string;
20
+ }
21
+ declare function isTocForSeriesToken(token: TocForSeriesToken | {
22
+ type: unknown;
23
+ }): token is TocForSeriesToken;
24
+
25
+ export { type ArticleDef, type MarkedTocForSeriesOptions, type SerieDef, type SeriesDef, isTocForSeriesToken, markedTocForSeries };
package/dist/index.mjs ADDED
@@ -0,0 +1,242 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __commonJS = (cb, mod) => function __require() {
8
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
19
+ // If the importer is in node compatibility mode or this is not an ESM
20
+ // file that has been converted to a CommonJS file using a Babel-
21
+ // compatible transform (i.e. "__esModule" has not been set), then set
22
+ // "default" to the CommonJS "module.exports" for node compatibility.
23
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
24
+ mod
25
+ ));
26
+
27
+ // ../fp/dist/basic.js
28
+ var require_basic = __commonJS({
29
+ "../fp/dist/basic.js"(exports) {
30
+ "use strict";
31
+ Object.defineProperty(exports, "__esModule", { value: true });
32
+ exports.notf = exports.not = exports.identity = exports.noop = void 0;
33
+ function noop() {
34
+ }
35
+ exports.noop = noop;
36
+ function identity(x) {
37
+ return x;
38
+ }
39
+ exports.identity = identity;
40
+ function not(x) {
41
+ return !x;
42
+ }
43
+ exports.not = not;
44
+ function notf(f) {
45
+ return (...args) => not(f(...args));
46
+ }
47
+ exports.notf = notf;
48
+ }
49
+ });
50
+
51
+ // ../fp/dist/predicates.js
52
+ var require_predicates = __commonJS({
53
+ "../fp/dist/predicates.js"(exports) {
54
+ "use strict";
55
+ Object.defineProperty(exports, "__esModule", { value: true });
56
+ exports.isNotDefinedOrEmpty = exports.isDefinedAndNotEmpty = exports.isNotEmpty = exports.isEmpty = exports.isNotDefined = exports.isDefined = void 0;
57
+ function isDefined2(x) {
58
+ return x != void 0;
59
+ }
60
+ exports.isDefined = isDefined2;
61
+ function isNotDefined2(x) {
62
+ return x == void 0;
63
+ }
64
+ exports.isNotDefined = isNotDefined2;
65
+ function isEmpty(x) {
66
+ return x.length === 0;
67
+ }
68
+ exports.isEmpty = isEmpty;
69
+ function isNotEmpty(x) {
70
+ return x.length > 0;
71
+ }
72
+ exports.isNotEmpty = isNotEmpty;
73
+ function isDefinedAndNotEmpty2(x) {
74
+ return isDefined2(x) && isNotEmpty(x);
75
+ }
76
+ exports.isDefinedAndNotEmpty = isDefinedAndNotEmpty2;
77
+ function isNotDefinedOrEmpty2(x) {
78
+ return isNotDefined2(x) || isEmpty(x);
79
+ }
80
+ exports.isNotDefinedOrEmpty = isNotDefinedOrEmpty2;
81
+ }
82
+ });
83
+
84
+ // ../fp/dist/clone.js
85
+ var require_clone = __commonJS({
86
+ "../fp/dist/clone.js"(exports) {
87
+ "use strict";
88
+ Object.defineProperty(exports, "__esModule", { value: true });
89
+ exports.deepClone = void 0;
90
+ var predicates_1 = require_predicates();
91
+ function deepClone(x) {
92
+ if (x instanceof Date) {
93
+ return new Date(x.getTime());
94
+ }
95
+ if (typeof x === "bigint") {
96
+ return BigInt(x.toString());
97
+ }
98
+ if ((0, predicates_1.isNotDefined)(x) || typeof x === "boolean" || typeof x === "number" || x === Number.NEGATIVE_INFINITY || x === Number.POSITIVE_INFINITY) {
99
+ return x;
100
+ }
101
+ if (Array.isArray(x)) {
102
+ return x.map(deepClone);
103
+ }
104
+ if (typeof x === "object") {
105
+ return Object.fromEntries(
106
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
107
+ Object.entries(x).map(([k, v]) => [
108
+ k,
109
+ deepClone(v)
110
+ ])
111
+ );
112
+ }
113
+ return JSON.parse(JSON.stringify(x));
114
+ }
115
+ exports.deepClone = deepClone;
116
+ }
117
+ });
118
+
119
+ // ../fp/dist/equality.js
120
+ var require_equality = __commonJS({
121
+ "../fp/dist/equality.js"(exports) {
122
+ "use strict";
123
+ Object.defineProperty(exports, "__esModule", { value: true });
124
+ exports.isDifferent = exports.isEqual = void 0;
125
+ var basic_1 = require_basic();
126
+ function isEqual(a, b) {
127
+ if (typeof a !== typeof b) {
128
+ return false;
129
+ }
130
+ if (typeof a === "boolean" || typeof a === "number" || typeof a === "function" || typeof a === "string" || typeof a === "undefined" || a === null) {
131
+ return a === b;
132
+ }
133
+ if (a instanceof Date && b instanceof Date) {
134
+ return a.getTime() === b.getTime();
135
+ }
136
+ let aa = a;
137
+ let bb = b;
138
+ if (typeof a === "bigint") {
139
+ aa = a.toString();
140
+ }
141
+ if (typeof b === "bigint") {
142
+ bb = b.toString();
143
+ }
144
+ return JSON.stringify(aa) === JSON.stringify(bb);
145
+ }
146
+ exports.isEqual = isEqual;
147
+ function isDifferent(a, b) {
148
+ return (0, basic_1.not)(isEqual(a, b));
149
+ }
150
+ exports.isDifferent = isDifferent;
151
+ }
152
+ });
153
+
154
+ // ../fp/dist/index.js
155
+ var require_dist = __commonJS({
156
+ "../fp/dist/index.js"(exports) {
157
+ "use strict";
158
+ var __createBinding = exports && exports.__createBinding || (Object.create ? function(o, m, k, k2) {
159
+ if (k2 === void 0) k2 = k;
160
+ var desc = Object.getOwnPropertyDescriptor(m, k);
161
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
162
+ desc = { enumerable: true, get: function() {
163
+ return m[k];
164
+ } };
165
+ }
166
+ Object.defineProperty(o, k2, desc);
167
+ } : function(o, m, k, k2) {
168
+ if (k2 === void 0) k2 = k;
169
+ o[k2] = m[k];
170
+ });
171
+ var __exportStar = exports && exports.__exportStar || function(m, exports2) {
172
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports2, p)) __createBinding(exports2, m, p);
173
+ };
174
+ Object.defineProperty(exports, "__esModule", { value: true });
175
+ __exportStar(require_basic(), exports);
176
+ __exportStar(require_clone(), exports);
177
+ __exportStar(require_equality(), exports);
178
+ __exportStar(require_predicates(), exports);
179
+ }
180
+ });
181
+
182
+ // src/marked-toc-for-series.ts
183
+ var import_fp = __toESM(require_dist());
184
+ function markedTocForSeries({
185
+ title,
186
+ series
187
+ }) {
188
+ return {
189
+ extensions: [
190
+ {
191
+ name: "tocForSeries",
192
+ level: "block",
193
+ start(src) {
194
+ var _a;
195
+ return (_a = src.match(/<!-- TOC-SERIE:.* -->/)) == null ? void 0 : _a.index;
196
+ },
197
+ tokenizer(src) {
198
+ const match = /^<!-- TOC-SERIE:(.*) -->?/.exec(src);
199
+ if ((0, import_fp.isDefined)(match)) {
200
+ const type = "tocForSeries";
201
+ const raw = src.slice(0, src.indexOf("-->") + 3);
202
+ const serieId = match == null ? void 0 : match[1];
203
+ const token = { type, raw, serieId };
204
+ return token;
205
+ }
206
+ return void 0;
207
+ },
208
+ renderer(token) {
209
+ if (!isTocForSeriesToken(token)) {
210
+ return false;
211
+ }
212
+ const serie = series[token.serieId];
213
+ if ((0, import_fp.isNotDefined)(serie) || (0, import_fp.isNotDefinedOrEmpty)(serie.articles)) {
214
+ return "";
215
+ }
216
+ const titleHtml = (0, import_fp.isDefinedAndNotEmpty)(title) ? `<h2>${title}</h2>` : void 0;
217
+ return rows(
218
+ `<nav data-serie="${token.serieId}">`,
219
+ titleHtml,
220
+ "<ul>",
221
+ ...serie.articles.map(
222
+ (article) => `<li><a href="${article.url}">${article.label}</a></li>`
223
+ ),
224
+ "</ul>",
225
+ "</nav>"
226
+ );
227
+ }
228
+ }
229
+ ]
230
+ };
231
+ }
232
+ function isTocForSeriesToken(token) {
233
+ return token.type === "tocForSeries";
234
+ }
235
+ function rows(...s) {
236
+ return s.filter(import_fp.isDefinedAndNotEmpty).join("\n") + "\n";
237
+ }
238
+ export {
239
+ isTocForSeriesToken,
240
+ markedTocForSeries
241
+ };
242
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../fp/dist/basic.js","../../fp/dist/predicates.js","../../fp/dist/clone.js","../../fp/dist/equality.js","../../fp/dist/index.js","../src/marked-toc-for-series.ts"],"sourcesContent":["\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.notf = exports.not = exports.identity = exports.noop = void 0;\nfunction noop() { }\nexports.noop = noop;\nfunction identity(x) {\n return x;\n}\nexports.identity = identity;\nfunction not(x) {\n return !x;\n}\nexports.not = not;\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction notf(f) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return ((...args) => not(f(...args)));\n}\nexports.notf = notf;\n","\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.isNotDefinedOrEmpty = exports.isDefinedAndNotEmpty = exports.isNotEmpty = exports.isEmpty = exports.isNotDefined = exports.isDefined = void 0;\nfunction isDefined(x) {\n return x != undefined;\n}\nexports.isDefined = isDefined;\nfunction isNotDefined(x) {\n return x == undefined;\n}\nexports.isNotDefined = isNotDefined;\nfunction isEmpty(x) {\n return x.length === 0;\n}\nexports.isEmpty = isEmpty;\nfunction isNotEmpty(x) {\n return x.length > 0;\n}\nexports.isNotEmpty = isNotEmpty;\nfunction isDefinedAndNotEmpty(x) {\n return isDefined(x) && isNotEmpty(x);\n}\nexports.isDefinedAndNotEmpty = isDefinedAndNotEmpty;\nfunction isNotDefinedOrEmpty(x) {\n return isNotDefined(x) || isEmpty(x);\n}\nexports.isNotDefinedOrEmpty = isNotDefinedOrEmpty;\n","\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.deepClone = void 0;\nconst predicates_1 = require(\"./predicates\");\nfunction deepClone(x) {\n if (x instanceof Date) {\n return new Date(x.getTime());\n }\n if (typeof x === 'bigint') {\n return BigInt(x.toString());\n }\n if ((0, predicates_1.isNotDefined)(x) ||\n typeof x === 'boolean' ||\n typeof x === 'number' ||\n x === Number.NEGATIVE_INFINITY ||\n x === Number.POSITIVE_INFINITY) {\n return x;\n }\n if (Array.isArray(x)) {\n return x.map(deepClone);\n }\n if (typeof x === 'object') {\n return Object.fromEntries(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n Object.entries(x).map(([k, v]) => [\n k,\n deepClone(v),\n ]));\n }\n return JSON.parse(JSON.stringify(x));\n}\nexports.deepClone = deepClone;\n","\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.isDifferent = exports.isEqual = void 0;\nconst basic_1 = require(\"./basic\");\nfunction isEqual(a, b) {\n if (typeof a !== typeof b) {\n return false;\n }\n if (typeof a === 'boolean' ||\n typeof a === 'number' ||\n typeof a === 'function' ||\n typeof a === 'string' ||\n typeof a === 'undefined' ||\n a === null) {\n return a === b;\n }\n if (a instanceof Date && b instanceof Date) {\n return a.getTime() === b.getTime();\n }\n let aa = a;\n let bb = b;\n if (typeof a === 'bigint') {\n aa = a.toString();\n }\n if (typeof b === 'bigint') {\n bb = b.toString();\n }\n return JSON.stringify(aa) === JSON.stringify(bb);\n}\nexports.isEqual = isEqual;\nfunction isDifferent(a, b) {\n return (0, basic_1.not)(isEqual(a, b));\n}\nexports.isDifferent = isDifferent;\n","\"use strict\";\nvar __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n var desc = Object.getOwnPropertyDescriptor(m, k);\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\n desc = { enumerable: true, get: function() { return m[k]; } };\n }\n Object.defineProperty(o, k2, desc);\n}) : (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n o[k2] = m[k];\n}));\nvar __exportStar = (this && this.__exportStar) || function(m, exports) {\n for (var p in m) if (p !== \"default\" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);\n};\nObject.defineProperty(exports, \"__esModule\", { value: true });\n__exportStar(require(\"./basic\"), exports);\n__exportStar(require(\"./clone\"), exports);\n__exportStar(require(\"./equality\"), exports);\n__exportStar(require(\"./predicates\"), exports);\n","import { marked } from 'marked';\nimport {\n isDefined,\n isDefinedAndNotEmpty,\n isNotDefined,\n isNotDefinedOrEmpty,\n} from '@anthonypena/fp';\n\nexport type SeriesDef = Record<string, SerieDef>;\n\nexport interface MarkedTocForSeriesOptions {\n title?: string | undefined | null;\n series: SeriesDef;\n}\n\nexport interface SerieDef {\n articles: ArticleDef[];\n}\n\nexport interface ArticleDef {\n label: string;\n url: string;\n}\n\nexport function markedTocForSeries({\n title,\n series,\n}: MarkedTocForSeriesOptions): marked.MarkedExtension {\n return {\n extensions: [\n {\n name: 'tocForSeries',\n level: 'block',\n start(src) {\n return src.match(/<!-- TOC-SERIE:.* -->/)?.index;\n },\n tokenizer(src): TocForSeriesToken | undefined {\n const match = /^<!-- TOC-SERIE:(.*) -->?/.exec(src);\n if (isDefined(match)) {\n const type = 'tocForSeries';\n const raw = src.slice(0, src.indexOf('-->') + 3);\n const serieId = match?.[1];\n const token: TocForSeriesToken = { type, raw, serieId };\n return token;\n }\n return undefined;\n },\n renderer(token) {\n if (!isTocForSeriesToken(token)) {\n return false;\n }\n const serie = series[token.serieId];\n if (isNotDefined(serie) || isNotDefinedOrEmpty(serie.articles)) {\n return '';\n }\n\n const titleHtml = isDefinedAndNotEmpty(title)\n ? `<h2>${title}</h2>`\n : undefined;\n return rows(\n `<nav data-serie=\"${token.serieId}\">`,\n titleHtml,\n '<ul>',\n ...serie.articles.map(\n (article) =>\n `<li><a href=\"${article.url}\">${article.label}</a></li>`,\n ),\n '</ul>',\n '</nav>',\n );\n },\n },\n ],\n };\n}\n\ninterface TocForSeriesToken extends marked.Tokens.Generic {\n type: 'tocForSeries';\n serieId: string;\n raw: string;\n}\n\nexport function isTocForSeriesToken(\n token: TocForSeriesToken | { type: unknown },\n): token is TocForSeriesToken {\n return token.type === 'tocForSeries';\n}\n\nfunction rows(...s: (string | undefined | null)[]): string {\n return s.filter(isDefinedAndNotEmpty).join('\\n') + '\\n';\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AACA,WAAO,eAAe,SAAS,cAAc,EAAE,OAAO,KAAK,CAAC;AAC5D,YAAQ,OAAO,QAAQ,MAAM,QAAQ,WAAW,QAAQ,OAAO;AAC/D,aAAS,OAAO;AAAA,IAAE;AAClB,YAAQ,OAAO;AACf,aAAS,SAAS,GAAG;AACjB,aAAO;AAAA,IACX;AACA,YAAQ,WAAW;AACnB,aAAS,IAAI,GAAG;AACZ,aAAO,CAAC;AAAA,IACZ;AACA,YAAQ,MAAM;AAEd,aAAS,KAAK,GAAG;AAEb,aAAQ,IAAI,SAAS,IAAI,EAAE,GAAG,IAAI,CAAC;AAAA,IACvC;AACA,YAAQ,OAAO;AAAA;AAAA;;;AClBf;AAAA;AAAA;AACA,WAAO,eAAe,SAAS,cAAc,EAAE,OAAO,KAAK,CAAC;AAC5D,YAAQ,sBAAsB,QAAQ,uBAAuB,QAAQ,aAAa,QAAQ,UAAU,QAAQ,eAAe,QAAQ,YAAY;AAC/I,aAASA,WAAU,GAAG;AAClB,aAAO,KAAK;AAAA,IAChB;AACA,YAAQ,YAAYA;AACpB,aAASC,cAAa,GAAG;AACrB,aAAO,KAAK;AAAA,IAChB;AACA,YAAQ,eAAeA;AACvB,aAAS,QAAQ,GAAG;AAChB,aAAO,EAAE,WAAW;AAAA,IACxB;AACA,YAAQ,UAAU;AAClB,aAAS,WAAW,GAAG;AACnB,aAAO,EAAE,SAAS;AAAA,IACtB;AACA,YAAQ,a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,GAAG,KAAK,QAAQ,KAAK;AAAA,YACjD;AAAA,YACA;AAAA,YACA;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAQO,SAAS,oBACd,OAC4B;AAC5B,SAAO,MAAM,SAAS;AACxB;AAEA,SAAS,QAAQ,GAA0C;AACzD,SAAO,EAAE,OAAO,8BAAoB,EAAE,KAAK,IAAI,IAAI;AACrD;","names":["isDefined","isNotDefined","isDefinedAndNotEmpty","isNotDefinedOrEmpty","exports"]}
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@anthonypena/marked-toc-for-series",
3
+ "version": "1.0.0",
4
+ "description": "Plugin add table of content for series of urls.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "private": false,
8
+ "keywords": [
9
+ "marked"
10
+ ],
11
+ "author": "Anthony Pena",
12
+ "license": "MIT",
13
+ "dependencies": {
14
+ "@anthonypena/fp": "1.0.1"
15
+ },
16
+ "devDependencies": {
17
+ "@types/marked": "^5.0.0",
18
+ "eslint": "^8.57.0",
19
+ "marked": "^5.0.0",
20
+ "npm-run-all": "^4.1.5",
21
+ "prettier": "^3.2.5",
22
+ "rimraf": "^5.0.5",
23
+ "tsup": "^8.5.0",
24
+ "typescript": "^5.4.3",
25
+ "vitest": "^1.4.0",
26
+ "@anthonypena/tsconfig": "0.1.0",
27
+ "@anthonypena/prettier-config": "0.1.0",
28
+ "@anthonypena/eslint-plugin": "0.1.0"
29
+ },
30
+ "peerDependencies": {
31
+ "marked": "=5.x"
32
+ },
33
+ "prettier": "@anthonypena/prettier-config",
34
+ "scripts": {
35
+ "clean": "rimraf ./dist",
36
+ "build-ts": "tsup src/index.ts",
37
+ "build": "run-s clean build-ts",
38
+ "test": "vitest",
39
+ "pretty": "prettier . --check",
40
+ "pretty-fix": "prettier . --write",
41
+ "lint": "eslint --ext ts,tsx,js,tsx src",
42
+ "lint-fix": "pnpm lint --fix"
43
+ }
44
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './marked-toc-for-series';
@@ -0,0 +1,55 @@
1
+ import { Marked } from 'marked';
2
+ import { describe, expect, it } from 'vitest';
3
+
4
+ import { markedTocForSeries } from './marked-toc-for-series';
5
+
6
+ describe(markedTocForSeries.name, () => {
7
+ it.each([
8
+ [
9
+ '<!-- TOC-SERIE:some -->',
10
+ undefined,
11
+ {
12
+ some: {
13
+ articles: [
14
+ { label: 'foo', url: 'http://example.com/foo' },
15
+ { label: 'bar baz', url: 'http://example.com/bar-baz' },
16
+ ],
17
+ },
18
+ },
19
+ '<nav data-serie="some">\n<ul>\n<li><a href="http://example.com/foo">foo</a></li>\n<li><a href="http://example.com/bar-baz">bar baz</a></li>\n</ul>\n</nav>\n',
20
+ ],
21
+ [
22
+ '<!-- TOC-SERIE:serie-id-with-dash -->',
23
+ 'Serie',
24
+ {
25
+ 'serie-id-with-dash': {
26
+ articles: [
27
+ { label: 'foo', url: 'http://example.com/foo' },
28
+ { label: 'bar baz', url: 'http://example.com/bar-baz' },
29
+ ],
30
+ },
31
+ },
32
+ '<nav data-serie="serie-id-with-dash">\n<h2>Serie</h2>\n<ul>\n<li><a href="http://example.com/foo">foo</a></li>\n<li><a href="http://example.com/bar-baz">bar baz</a></li>\n</ul>\n</nav>\n',
33
+ ],
34
+ [
35
+ '<!-- TOC-SERIE:serie_id_with_underscore -->',
36
+ 'Série',
37
+ {
38
+ serie_id_with_underscore: {
39
+ articles: [
40
+ { label: 'foo', url: 'http://example.com/foo' },
41
+ { label: 'bar baz', url: 'http://example.com/bar-baz' },
42
+ ],
43
+ },
44
+ },
45
+ '<nav data-serie="serie_id_with_underscore">\n<h2>Série</h2>\n<ul>\n<li><a href="http://example.com/foo">foo</a></li>\n<li><a href="http://example.com/bar-baz">bar baz</a></li>\n</ul>\n</nav>\n',
46
+ ],
47
+ ['<!-- TOC-SERIE:empty -->', 'Yolo', { empty: { articles: [] } }, ''],
48
+ ['<!-- TOC-SERIE:not-existing -->', '', { some: { articles: [] } }, ''],
49
+ ])('should create TOC "%s"', (md, title, series, html) => {
50
+ const marked = new Marked({ headerIds: false, mangle: false });
51
+ marked.use(markedTocForSeries({ title, series }));
52
+
53
+ expect(marked.parse(md)).toBe(html);
54
+ });
55
+ });
@@ -0,0 +1,91 @@
1
+ import { marked } from 'marked';
2
+ import {
3
+ isDefined,
4
+ isDefinedAndNotEmpty,
5
+ isNotDefined,
6
+ isNotDefinedOrEmpty,
7
+ } from '@anthonypena/fp';
8
+
9
+ export type SeriesDef = Record<string, SerieDef>;
10
+
11
+ export interface MarkedTocForSeriesOptions {
12
+ title?: string | undefined | null;
13
+ series: SeriesDef;
14
+ }
15
+
16
+ export interface SerieDef {
17
+ articles: ArticleDef[];
18
+ }
19
+
20
+ export interface ArticleDef {
21
+ label: string;
22
+ url: string;
23
+ }
24
+
25
+ export function markedTocForSeries({
26
+ title,
27
+ series,
28
+ }: MarkedTocForSeriesOptions): marked.MarkedExtension {
29
+ return {
30
+ extensions: [
31
+ {
32
+ name: 'tocForSeries',
33
+ level: 'block',
34
+ start(src) {
35
+ return src.match(/<!-- TOC-SERIE:.* -->/)?.index;
36
+ },
37
+ tokenizer(src): TocForSeriesToken | undefined {
38
+ const match = /^<!-- TOC-SERIE:(.*) -->?/.exec(src);
39
+ if (isDefined(match)) {
40
+ const type = 'tocForSeries';
41
+ const raw = src.slice(0, src.indexOf('-->') + 3);
42
+ const serieId = match?.[1];
43
+ const token: TocForSeriesToken = { type, raw, serieId };
44
+ return token;
45
+ }
46
+ return undefined;
47
+ },
48
+ renderer(token) {
49
+ if (!isTocForSeriesToken(token)) {
50
+ return false;
51
+ }
52
+ const serie = series[token.serieId];
53
+ if (isNotDefined(serie) || isNotDefinedOrEmpty(serie.articles)) {
54
+ return '';
55
+ }
56
+
57
+ const titleHtml = isDefinedAndNotEmpty(title)
58
+ ? `<h2>${title}</h2>`
59
+ : undefined;
60
+ return rows(
61
+ `<nav data-serie="${token.serieId}">`,
62
+ titleHtml,
63
+ '<ul>',
64
+ ...serie.articles.map(
65
+ (article) =>
66
+ `<li><a href="${article.url}">${article.label}</a></li>`,
67
+ ),
68
+ '</ul>',
69
+ '</nav>',
70
+ );
71
+ },
72
+ },
73
+ ],
74
+ };
75
+ }
76
+
77
+ interface TocForSeriesToken extends marked.Tokens.Generic {
78
+ type: 'tocForSeries';
79
+ serieId: string;
80
+ raw: string;
81
+ }
82
+
83
+ export function isTocForSeriesToken(
84
+ token: TocForSeriesToken | { type: unknown },
85
+ ): token is TocForSeriesToken {
86
+ return token.type === 'tocForSeries';
87
+ }
88
+
89
+ function rows(...s: (string | undefined | null)[]): string {
90
+ return s.filter(isDefinedAndNotEmpty).join('\n') + '\n';
91
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "@anthonypena/tsconfig/base.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "lib": ["ES2021"],
6
+ "module": "CommonJS"
7
+ },
8
+ "include": ["src/**/*.ts"],
9
+ "exclude": ["dist", "node_modules", "src/**/*.spec.ts"]
10
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { defineConfig } from 'tsup';
2
+
3
+ export default defineConfig({
4
+ entry: ['src/index.ts'],
5
+ splitting: false,
6
+ sourcemap: true,
7
+ format: 'esm',
8
+ clean: true,
9
+ dts: true,
10
+ bundle: true,
11
+ noExternal: [/^@anthonypena\/.*/],
12
+ });