@ng-doc/utils 22.0.0-beta.1 → 22.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/README.md CHANGED
@@ -1,37 +1,28 @@
1
- <!-- PROJECT LOGO -->
2
- <br />
3
- <div align="center">
4
- <a href="https://github.com/ng-doc/ng-doc">
5
- <img src="https://ng-doc.com/assets/images/ng-doc.svg?raw=true" alt="Logo" height="150px">
6
- </a>
7
-
8
- <h1 align="center" style="margin-bottom: 0; border-bottom: 0">NgDoc</h1>
9
- <p align="center">
10
- Create user-friendly documentation for your projects with ease!
11
- <br />
12
- <a href="https://ng-doc.com/">Documentation</a>
13
- ·
14
- <a href="https://github.com/ng-doc/ng-doc/issues/new/choose">Report Bug</a>
15
- ·
16
- <a href="https://github.com/ng-doc/ng-doc/issues/new/choose">Request Feature</a>
17
-
18
- [![GitHub Workflow Status][build-shield]][build-url]
1
+ # 🧰 @ng-doc/utils
2
+
3
+ Small utilities shared by the other NgDoc packages.
4
+
5
+ It's part of [NgDoc](https://ng-doc.com/) 📚, the documentation engine for Angular projects.
6
+
19
7
  [![NPM][npm-shield]][npm-url]
20
8
  [![MIT License][license-shield]][license-url]
21
9
 
22
- </p>
23
- </div>
10
+ ## 🚀 Getting started
11
+
12
+ You don't need to install this package by hand. Add NgDoc to your project and it sets up
13
+ everything it needs:
24
14
 
25
- <!-- ABOUT THE PROJECT -->
15
+ ```bash
16
+ ng add @ng-doc/add
17
+ ```
26
18
 
27
- ## About The Project
19
+ ## 📖 Learn more
28
20
 
29
- This library is a part of NgDoc project, that uses to create documentation
30
- for Angular projects, please check our [repo](https://github.com/ng-doc/ng-doc) for more details.
21
+ - [Documentation](https://ng-doc.com/)
22
+ - [GitHub repository](https://github.com/ng-doc/ng-doc)
23
+ - [Report a bug or request a feature](https://github.com/ng-doc/ng-doc/issues/new/choose)
31
24
 
32
- [npm-shield]: https://img.shields.io/npm/v/@ng-doc/builder.svg?style=for-the-badge
33
- [npm-url]: https://www.npmjs.com/package/@ng-doc/builder
25
+ [npm-shield]: https://img.shields.io/npm/v/%40ng-doc%2Futils.svg?style=for-the-badge
26
+ [npm-url]: https://www.npmjs.com/package/@ng-doc/utils
34
27
  [license-shield]: https://img.shields.io/github/license/ng-doc/ng-doc.svg?style=for-the-badge
35
- [license-url]: https://github.com/othneildrew/Best-README-Template/blob/master/LICENSE.txt
36
- [build-shield]: https://img.shields.io/github/actions/workflow/status/ng-doc/ng-doc/release.yml?style=for-the-badge&branch=release
37
- [build-url]: https://github.com/ng-doc/ng-doc/actions
28
+ [license-url]: https://github.com/ng-doc/ng-doc/blob/main/LICENSE
@@ -0,0 +1,68 @@
1
+ import { type RehypeShikiOptions } from '@shikijs/rehype';
2
+ import type { Root } from 'hast';
3
+ /**
4
+ * Everything a highlighted code block depends on, apart from the installed Shiki release. A cache
5
+ * keys a block by it (plus that release) with {@link NgDocHighlightCache.key}.
6
+ */
7
+ export interface NgDocHighlightBlock {
8
+ /** The plugin's format and the fixed Shiki options it highlights with. */
9
+ readonly options: Readonly<Record<string, string | number | boolean>>;
10
+ /** The theme names. NgDoc's own theme (`css-variables`) is not a Shiki theme. */
11
+ readonly themes: {
12
+ readonly light: string;
13
+ readonly dark: string;
14
+ };
15
+ /** The language of the block's class, or the default language: before the fallback. */
16
+ readonly lang: string;
17
+ /** The raw meta string, before it is parsed. */
18
+ readonly meta: string;
19
+ /** The block's text. */
20
+ readonly code: string;
21
+ }
22
+ /**
23
+ * A cache of highlighted code blocks for `processHtml`. Values are the JSON of the HAST nodes that
24
+ * replace a block's `pre` element. A value that is not a JSON array of HAST elements is a miss.
25
+ * The cache only ever receives values of blocks that were highlighted without an error.
26
+ */
27
+ export interface NgDocHighlightCache {
28
+ /** The key of a block. It must change whenever anything in the block or the Shiki release does. */
29
+ key(block: NgDocHighlightBlock): string;
30
+ get(key: string): string | undefined;
31
+ set(key: string, value: string): void;
32
+ /**
33
+ * Whether these themes are known to load, because the cache holds a block highlighted with them.
34
+ * Without that, a document is processed only after Shiki has loaded them, as without a cache, so
35
+ * that a theme that cannot load fails every document, even one without code.
36
+ */
37
+ loads?(themes: NgDocHighlightBlock['themes']): boolean;
38
+ /** Highlights every hit again, uses the fresh value and reports each difference. */
39
+ readonly verify?: boolean;
40
+ /** A hit whose fresh value differs (with `verify`). */
41
+ mismatch?(key: string): void;
42
+ }
43
+ /** The options of {@link cachedShikiPlugin}. */
44
+ export interface CachedShikiOptions {
45
+ cache: NgDocHighlightCache;
46
+ themes: NgDocHighlightBlock['themes'];
47
+ /** What `options` stands for in a key: change it whenever the options or this plugin change. */
48
+ identity: NgDocHighlightBlock['options'];
49
+ /** The language of a block without a `language-*` class (the options' `defaultLanguage`). */
50
+ defaultLanguage: string;
51
+ /** The options `@shikijs/rehype` highlights with, created when the highlighter is. */
52
+ options: () => RehypeShikiOptions;
53
+ }
54
+ /**
55
+ * The plain `@shikijs/rehype` plugin, with a cache of its results per block.
56
+ *
57
+ * It selects the blocks exactly as the plugin does (`pre` elements whose first child is a `code`
58
+ * element with properties, with the language of its `language-*` class or the default language)
59
+ * and leaves the tree unchanged while it keys them. A miss is highlighted by the plugin's own
60
+ * transformer, on a tree that holds only that block, so it gets exactly the plugin's options,
61
+ * language fallback, meta parsing and transformers. A failing block throws the plugin's error at
62
+ * the same block, since a hit cannot fail (the cache holds only blocks that were highlighted), and
63
+ * nothing is stored for it. Every value is serialized before the tree is changed, and every block
64
+ * is replaced with nodes parsed from it, so later plugins, which change the tree, never change a
65
+ * value or a node another block shares. A document whose blocks all hit (or that has none) never
66
+ * loads Shiki once the cache shows that its themes load.
67
+ */
68
+ export default function cachedShikiPlugin(settings: CachedShikiOptions): (tree: Root) => Promise<void>;
@@ -1,9 +1,18 @@
1
1
  import { NgDocHeading, NgDocPageAnchor } from '@ng-doc/core';
2
+ import { NgDocHighlightCache } from './plugins/cached-shiki.plugin';
3
+ export type { NgDocHighlightBlock, NgDocHighlightCache } from './plugins/cached-shiki.plugin';
2
4
  export interface NgDocHtmlProcessorConfig {
3
5
  lightTheme?: string;
4
6
  darkTheme?: string;
5
7
  headings?: NgDocHeading[];
6
8
  route?: string;
9
+ /**
10
+ * A cache of highlighted code blocks. With it, code is highlighted by a Shiki highlighter that is
11
+ * set up once per thread and theme pair, and a block is highlighted only when the cache misses;
12
+ * the result is the same as without it. Without it, every call sets the highlighter up again
13
+ * and highlights every block.
14
+ */
15
+ highlight?: NgDocHighlightCache;
7
16
  }
8
17
  export interface NgDocHtmlProcessorOutput {
9
18
  content: string;