@p-buddy/parkdown 0.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.
Files changed (43) hide show
  1. package/.assets/api-note.md +3 -0
  2. package/.assets/api.md +34 -0
  3. package/.assets/authoring.md +69 -0
  4. package/.assets/code/depopulate.ts +6 -0
  5. package/.assets/code/inclusions.ts +6 -0
  6. package/.assets/depopulated.md +25 -0
  7. package/.assets/invocation.md +16 -0
  8. package/.assets/populated/block.md +9 -0
  9. package/.assets/populated/inline.multi.md +5 -0
  10. package/.assets/populated/inline.single.md +3 -0
  11. package/.assets/query.md +73 -0
  12. package/.assets/remap-imports.md +0 -0
  13. package/.assets/unpopulated/block.md +5 -0
  14. package/.assets/unpopulated/inline.multi.md +3 -0
  15. package/.assets/unpopulated/inline.single.md +1 -0
  16. package/.devcontainer/Dockerfile +16 -0
  17. package/.devcontainer/devcontainer.json +35 -0
  18. package/LICENSE +21 -0
  19. package/README.md +418 -0
  20. package/dist/cli.js +14 -0
  21. package/dist/index.d.ts +7 -0
  22. package/dist/index.js +396 -0
  23. package/dist/index.umd.cjs +15 -0
  24. package/package.json +42 -0
  25. package/src/api/index.test.ts +32 -0
  26. package/src/api/index.ts +8 -0
  27. package/src/api/types.ts +78 -0
  28. package/src/api/utils.test.ts +132 -0
  29. package/src/api/utils.ts +161 -0
  30. package/src/cli.ts +31 -0
  31. package/src/include.test.ts +369 -0
  32. package/src/include.ts +252 -0
  33. package/src/index.ts +35 -0
  34. package/src/region.test.ts +145 -0
  35. package/src/region.ts +138 -0
  36. package/src/remap.test.ts +37 -0
  37. package/src/remap.ts +72 -0
  38. package/src/utils.test.ts +238 -0
  39. package/src/utils.ts +184 -0
  40. package/src/wrap.ts +61 -0
  41. package/tsconfig.json +5 -0
  42. package/vite.cli.config.ts +23 -0
  43. package/vite.config.ts +20 -0
package/README.md ADDED
@@ -0,0 +1,418 @@
1
+ # parkdown (p▼)
2
+
3
+ `parkdown` allows you to include other file's content within your markdown using a link with no text (i.e. `[](<url>)`), where `<url>` corresponds to either:
4
+ - a local file, e.g. `[](./other.md)` or `[](../root.ts)`
5
+ - **_COMING SOON_**: An external link
6
+
7
+ Markdown renderers shouldn't display these links, but [parkdown]() can process and populate them. Also, your editor hopefully makes these links easy to navigate to, improving productivity.
8
+
9
+ Collectively, [parkdown]() enables your documentation to behave a little more like code, and for your code to have a rightful place in your documentation.
10
+
11
+ [](./.assets/invocation.md)
12
+ <!-- p▼ BEGIN -->
13
+ ## Invocation
14
+
15
+ Invoke [parkdown's]() functionality with either the [cli](#cli-inclusions) or via the `processMarkdownIncludes` [export](#populateMarkdownIncludes-export):
16
+
17
+ ### CLI
18
+
19
+ The following commands are all equivalent:
20
+ ```bash
21
+ npx parkdown --file ./README.md
22
+ npx parkdown -f README.md
23
+ npx parkdown # defaults to processing inclusions in the 'README.md' file of the current working directory
24
+ ```
25
+
26
+ ### `populateMarkdownIncludes` export
27
+
28
+ [](.assets/code/inclusions.ts?region=replace(pkg,'''parkdown'''))
29
+ <!-- p▼ BEGIN -->
30
+ ```ts
31
+ import { populateMarkdownInclusions } from "parkdown";
32
+
33
+ const file = "README.md";
34
+ const writeFile = true;
35
+
36
+ populateMarkdownInclusions(file, writeFile);
37
+ ```
38
+ <!-- p▼ END -->
39
+ <!-- p▼ END -->
40
+
41
+ [](./.assets/authoring.md)
42
+ <!-- p▼ BEGIN -->
43
+ ## Authoring
44
+
45
+ You author inclusions in your markdown files using a link with no text i.e. `[](<url>)`, where `<url>` points to some local or remote text resource (e.g.`./other.md`, `https://example.com/remote.md`).
46
+
47
+ These links can be rendered either [inline](#inline) or [block](#block), depending on how you author them.
48
+
49
+ ### Inline
50
+
51
+ Inline inclusions occur when your _text-less_ link has 1 or more siblings (meaning it's **not** the only node in a [paragraph](https://www.markdownguide.org/basic-syntax/#paragraphs-1)).
52
+
53
+ There are two equivalent ways to author inline inclusions, [single-line](#single-line) or [multi-line](#multi-line), and which you choose depends solely on how you want your raw markdown to look (it will **not** affect the rendered output).
54
+
55
+ #### Single-line
56
+
57
+ What you write:
58
+
59
+ [](.assets/unpopulated/inline.single.md?wrap=code)
60
+ <!-- p▼ BEGIN -->
61
+ ```md
62
+ Before... [](<url>) ...After
63
+ ```
64
+ <!-- p▼ END -->
65
+
66
+ What is rendered (**_before_** processing, same as [Option B](#option-b-multi-line)):
67
+
68
+ [](.assets/unpopulated/inline.single.md?wrap=quote&inline)
69
+ <!-- p▼ BEGIN -->
70
+ > Before... [](<url>) ...After
71
+ <!-- p▼ END -->
72
+
73
+ What your markdown file contains (**_after_** processing):
74
+
75
+ [](.assets/populated/inline.single.md?wrap=code)
76
+ <!-- p▼ BEGIN -->
77
+ ```md
78
+ Before... [](<url>) <!-- p▼ Begin -->
79
+ ...Included Content...
80
+ ...Included Content... <!-- p▼ End --> ...After
81
+ ```
82
+ <!-- p▼ END -->
83
+
84
+ What is rendered (**_after_** processing, same as [Option B](#option-b-multi-line)):
85
+
86
+ [](.assets/populated/inline.single.md?wrap=quote&inline)
87
+ <!-- p▼ BEGIN -->
88
+ > Before... [](<url>) <!-- p▼ Begin -->
89
+ ...Included Content...
90
+ ...Included Content... <!-- p▼ End --> ...After
91
+ <!-- p▼ END -->
92
+
93
+ #### Multi-line
94
+
95
+ What you write:
96
+
97
+ [](.assets/unpopulated/inline.multi.md?wrap=code)
98
+ <!-- p▼ BEGIN -->
99
+ ```md
100
+ Before...
101
+ [](<url>)
102
+ ...After
103
+ ```
104
+ <!-- p▼ END -->
105
+
106
+ What is rendered (**_before_** processing, same as [Option A](#option-a-single-line)):
107
+
108
+ [](.assets/unpopulated/inline.multi.md?wrap=quote&inline)
109
+ <!-- p▼ BEGIN -->
110
+ > Before...
111
+ [](<url>)
112
+ ...After
113
+ <!-- p▼ END -->
114
+
115
+ What your markdown file contains (**_after_** processing):
116
+
117
+ [](.assets/populated/inline.multi.md?wrap=code)
118
+ <!-- p▼ BEGIN -->
119
+ ```md
120
+ Before...
121
+ [](<url>) <!-- p▼ Begin -->
122
+ ...Included Content...
123
+ ...Included Content... <!-- p▼ End -->
124
+ ...After
125
+ ```
126
+ <!-- p▼ END -->
127
+
128
+ What is rendered (**_after_** processing, same as [Option A](#option-a-single-line)):
129
+
130
+ [](.assets/populated/inline.multi.md?wrap=quote&inline)
131
+ <!-- p▼ BEGIN -->
132
+ > Before...
133
+ [](<url>) <!-- p▼ Begin -->
134
+ ...Included Content...
135
+ ...Included Content... <!-- p▼ End -->
136
+ ...After
137
+ <!-- p▼ END -->
138
+
139
+ ### Block
140
+
141
+ Block inclusions occur when your "empty" link is the **only** node in a [paragraph](https://www.markdownguide.org/basic-syntax/#paragraphs-1) (at least before being populated). This is likely the most common way to author inclusions.
142
+
143
+ What you write:
144
+
145
+ [](.assets/unpopulated/block.md?wrap=code)
146
+ <!-- p▼ BEGIN -->
147
+ ```md
148
+ Before...
149
+
150
+ [](<url>)
151
+
152
+ ...After
153
+ ```
154
+ <!-- p▼ END -->
155
+
156
+ What is rendered (**_before_** processing):
157
+
158
+ [](.assets/unpopulated/block.md?wrap=quote)
159
+ <!-- p▼ BEGIN -->
160
+ <blockquote>
161
+
162
+ Before...
163
+
164
+ [](<url>)
165
+
166
+ ...After
167
+
168
+ </blockquote>
169
+
170
+ <!-- p▼ END -->
171
+
172
+ What your markdown file contains (**_after_** processing):
173
+
174
+ [](.assets/populated/block.md?wrap=code)
175
+ <!-- p▼ BEGIN -->
176
+ ```md
177
+ Before...
178
+
179
+ [](<url>)
180
+ <!-- p▼ Begin -->
181
+ ...Included Content...
182
+ ...Included Content...
183
+ <!-- p▼ End -->
184
+
185
+ ...After
186
+ ```
187
+ <!-- p▼ END -->
188
+
189
+ What is rendered (**_after_** processing):
190
+
191
+ [](.assets/populated/block.md?wrap=quote)
192
+ <!-- p▼ BEGIN -->
193
+ <blockquote>
194
+
195
+ Before...
196
+
197
+ [](<url>)
198
+ <!-- p▼ Begin -->
199
+ ...Included Content...
200
+ ...Included Content...
201
+ <!-- p▼ End -->
202
+
203
+ ...After
204
+
205
+ </blockquote>
206
+
207
+ <!-- p▼ END -->
208
+
209
+ [](.assets/query.md?heading=-1)
210
+ <!-- p▼ BEGIN -->
211
+ ### Query parameters
212
+
213
+ You can pass query parameters to your inclusion links to control how their content is processed and included within your markdown.
214
+
215
+ #### Processing Order
216
+
217
+ [](src/include.ts?&region=extract(query))
218
+ <!-- p▼ BEGIN -->
219
+ ```ts
220
+ const params = new URLSearchParams(query);
221
+ const regions = params.get("region")?.split(COMMA_NOT_IN_PARENTHESIS);
222
+ const skip = params.has("skip");
223
+ const headingModfiier = params.get("heading") ?? 0;
224
+ const inlineOverride = params.has("inline");
225
+ const wraps = params.get("wrap")?.split(COMMA_NOT_IN_PARENTHESIS);
226
+ ```
227
+ <!-- p▼ END -->
228
+
229
+ #### `region`
230
+
231
+ Either extract, remove, or replace content from the included file based on the provided specifier(s).
232
+
233
+ Specifiers will be searched for within the file's comments, and are expected to come in pairs / bookend the desired region, like so:
234
+
235
+ ```ts
236
+ /** some-specifier */
237
+ ... code to find ...
238
+ /** some-specifier */
239
+ ```
240
+
241
+ ```md
242
+ [](...?region=extract(some-specifier))
243
+ ```
244
+
245
+ Below is the currently supported API for the `region` query parameter, where each defined method signature can be _invoked_ as a value for the `region` parameter (e.g. `[](<url>?region=extract(some-specifier))`, `[](<url>?region=remove(some-specifier))`, `[](<url>?region=replace(some-specifier))`).
246
+
247
+ [](.assets/api-note.md?wrap=quote)
248
+ <!-- p▼ BEGIN -->
249
+ <blockquote>
250
+
251
+ **_NOTE ON API USAGE:_** As you can see from the included examples, each _invocation_ of an API method looks like a less strict (more quirky) version of a typical javascript function invocation.
252
+
253
+ Please see the [full explanation](#query-parameters-with-function-like-apis) to learn more and/or if the below is confusing.
254
+
255
+ </blockquote>
256
+
257
+ <!-- p▼ END -->
258
+
259
+ [](src/region.ts?region=extract(definition))
260
+ <!-- p▼ BEGIN -->
261
+ ```ts
262
+ const definitions = [
263
+ "extract(id: string, 0?: string, 1?: string, 2?: string)",
264
+ "remove(id: string, 0?: string, 1?: string, 2?: string)",
265
+ "replace(id: string, with?: string, space?: string)",
266
+ ]
267
+ ```
268
+ <!-- p▼ END -->
269
+
270
+ #### `skip`
271
+
272
+ Skip the default processing behavior for the given type of file.
273
+
274
+ [](src/include.ts?wrap=dropdown(See-default-processing-behavior.)&region=extract(Default-Behavior),replace(...))
275
+ <!-- p▼ BEGIN -->
276
+
277
+ <details>
278
+ <summary>See default processing behavior.</summary>
279
+
280
+ ```ts
281
+ if (extension === "md") {
282
+ ...
283
+ content = recursivelyPopulateInclusions(content, ...);
284
+ }
285
+ else if (/^(js|ts)x?|svelte$/i.test(extension))
286
+ content = wrap(content, "code", ...);
287
+ ```
288
+ </details>
289
+
290
+ <!-- p▼ END -->
291
+
292
+ ```md
293
+ [](<url>?skip)
294
+ ```
295
+
296
+ #### `heading`
297
+
298
+ Modify the heading depth applied to included content. By default, the headings of the included content are adjusted to be one-level below their parent heading.
299
+
300
+ In the following example, the headings within the included content of `<url>` will be adjusted to one-level below the parent heading (which is an `h2` / `##`), so any `#` headings will be converted to `###` headings, and `##` headings will be converted to `####` headings, and so on.
301
+
302
+ ```md
303
+ ## Heading
304
+
305
+ [](<url>)
306
+ ```
307
+
308
+ The following would then ensure that the headings of the included content are at the same level as the parent heading.
309
+
310
+ ```md
311
+ ## Heading
312
+
313
+ [](<url>?heading=-1)
314
+ ```
315
+
316
+ A value of `-2` would result in the headings of the included content being at their original level (since the content is being included underneath an `h2` / `##` heading).
317
+
318
+ #### `inline` (Advanced)
319
+
320
+ #### `wrap`
321
+
322
+ Wrap the content of the included file in a specific kind of element.
323
+
324
+ Below is the currently supported API for the `wrap` query parameter, where each defined method signature can be _invoked_ as a value for the `wrap` parameter (e.g. `[](<url>?wrap=code)`, `[](<url>?wrap=dropdown(hello-world))`).
325
+
326
+ [](.assets/api-note.md?wrap=quote)
327
+ <!-- p▼ BEGIN -->
328
+ <blockquote>
329
+
330
+ **_NOTE ON API USAGE:_** As you can see from the included examples, each _invocation_ of an API method looks like a less strict (more quirky) version of a typical javascript function invocation.
331
+
332
+ Please see the [full explanation](#query-parameters-with-function-like-apis) to learn more and/or if the below is confusing.
333
+
334
+ </blockquote>
335
+
336
+ <!-- p▼ END -->
337
+
338
+ [](src/wrap.ts?region=extract(definition))
339
+ <!-- p▼ BEGIN -->
340
+ ```ts
341
+ const definitions = [
342
+ /**
343
+ * Wraps the content in a markdown-formatted code block.
344
+ * @param lang The language of the code block (defaults to the file extension).
345
+ * @param meta Additional metadata to include in the top line of the code block (i.e. to the right of the `lang`).
346
+ * @example [](<url>?wrap=code)
347
+ * @example [](<url>?wrap=code())
348
+ * @example [](<url>?wrap=code(ts))
349
+ * @example [](<url>?wrap=code(,some-meta))
350
+ */
351
+ "code(lang?: string, meta?: string)",
352
+
353
+ /**
354
+ * Wraps the content in a markdown-formatted blockquote
355
+ * (using the `>` character if the content is a single line,
356
+ * or the `<blockquote>` tag if the content is a multi-line block).
357
+ * @example [](<url>?wrap=quote)
358
+ * @example [](<url>?wrap=quote())
359
+ * @example [](<url>?wrap=quote(,))
360
+ */
361
+ "quote()",
362
+
363
+ /**
364
+ * Wraps the content in a markdown-formatted dropdown (using the `<details>` and `<summary>` tags).
365
+ * @param summary The summary text of the dropdown.
366
+ * @param open Whether the dropdown should be open by default.
367
+ * @param space The space character to use between words in the summary (defaults to `-`).
368
+ * @example [](<url>?wrap=dropdown(hello-world))
369
+ * @example [](<url>?wrap=dropdown('hello,-world',true))
370
+ * @example [](<url>?wrap=dropdown(hello_world,,_))
371
+ */
372
+ "dropdown(summary: string, open?: boolean, space?: string)",
373
+
374
+ ]
375
+ ```
376
+ <!-- p▼ END -->
377
+
378
+ <!-- p▼ END -->
379
+ <!-- p▼ END -->
380
+
381
+ [](./.assets/depopulated.md)
382
+ <!-- p▼ BEGIN -->
383
+ ## Removing populated inclusions
384
+
385
+ Sometimes you may want to remove populated inclusions from your markdown file, since they can make things more difficult to read during authoring. You can do this either using the [cli](#cli-removing-populated-inclusions) or via the `removePopulatedInclusions` [export](#depopulateMarkdownIncludes-export):
386
+
387
+ ### CLI (removing populated inclusions)
388
+
389
+ The following commands are all equivalent:
390
+
391
+ ```bash
392
+ npx parkdown --file ./README.md --depopulate --no-inclusions
393
+ npx parkdown -f README.md -d --ni # Notice the double-dash (--) on 'ni'
394
+ npx parkdown -d --ni # defaults to processing the 'README.md' file of the current working directory
395
+ ```
396
+
397
+ The following commands will lead to the same result, but since `--no-inclusions` (`--ni`) is not specified, there will be some wasted work as inclusions will be processed and then removed.
398
+
399
+ ```bash
400
+ npx parkdown --file ./README.md --depopulate
401
+ npx parkdown -f README.md -d
402
+ npx parkdown -d # defaults to processing the 'README.md' file of the current working directory
403
+ ```
404
+
405
+ ### `depopulateMarkdownIncludes` export
406
+
407
+ [](.assets/code/depopulate.ts?region=replace(pkg,'''parkdown'''))
408
+ <!-- p▼ BEGIN -->
409
+ ```ts
410
+ import { depopulateMarkdownInclusions } from "parkdown";
411
+
412
+ const file = "README.md";
413
+ const writeFile = true;
414
+
415
+ depopulateMarkdownInclusions(file, writeFile);
416
+ ```
417
+ <!-- p▼ END -->
418
+ <!-- p▼ END -->
package/dist/cli.js ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env node
2
+ import { Command as t } from "@commander-js/extra-typings";
3
+ import { populateMarkdownInclusions as r, depopulateMarkdownInclusions as l } from "parkdown";
4
+ const p = "0.0.0", f = new t().version(p).option("--nw, --no-write", "Do NOT write result to file (defaults to false)", !1).option("--ni, --no-inclusions", "Do NOT process file inclusions (defaults to false)", !1).option("-d, --depopulate", "Remove populated inclusions from the file", !1).option("-f, --file <flag>", "The file(s) to process", (e, o) => (o.push(e), o), new Array()).option("-r, --remap-imports", "Remap import specifiers in code blocks from one destination to another").parse(), { inclusions: a, depopulate: c, file: n, write: i } = f.opts();
5
+ n.length === 0 && n.push("README.md");
6
+ const u = [
7
+ [r, !a],
8
+ [l, c]
9
+ ];
10
+ for (const [e] of u.filter(([o, s]) => s))
11
+ for (const o of n) {
12
+ const s = e(o, !i);
13
+ i && console.log(s);
14
+ }
@@ -0,0 +1,7 @@
1
+ export declare const depopulateMarkdownInclusions: (file: string, writeFile?: boolean) => string;
2
+
3
+ export declare const populateMarkdownInclusions: (file: string, writeFile?: boolean) => string;
4
+
5
+ export declare const remapImportSpecifiers: (file: string, writeFile?: boolean) => void;
6
+
7
+ export { }