comark-reading-time 0.0.0 → 0.7.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 onmax
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 CHANGED
@@ -1,3 +1,24 @@
1
1
  # comark-reading-time
2
2
 
3
- Placeholder. The first release is published from https://github.com/onmax/comark-plugins.
3
+ [![npm version](https://comark-ui.onmax.me/badge.svg?source=npm-version&package=comark-reading-time&icon=npm)](https://npmx.dev/package/comark-reading-time)
4
+ [![npm downloads](https://comark-ui.onmax.me/badge.svg?source=npm-downloads&package=comark-reading-time&period=month&icon=npm)](https://npmx.dev/package/comark-reading-time)
5
+ [![license](https://comark-ui.onmax.me/badge.svg?label=license&value=MIT)](https://npmx.dev/package/comark-reading-time)
6
+ [![docs](https://comark-ui.onmax.me/badge.svg?label=docs&value=comark-ui.onmax.me)](https://comark-ui.onmax.me/reading-time/)
7
+
8
+ A document's word count and reading time, written to `meta.readingTime` while parsing. A [Comark](https://comark.dev) plugin for any renderer.
9
+
10
+ ```sh
11
+ pnpm add comark comark-reading-time
12
+ ```
13
+
14
+ ```ts
15
+ import { parseMarkdown } from "comark";
16
+ import readingTime from "comark-reading-time";
17
+
18
+ const { meta } = await parseMarkdown(markdown, { plugins: [readingTime({ wordsPerMinute: 200 })] });
19
+ meta.readingTime; // { words: 1240, minutes: 7 }
20
+ ```
21
+
22
+ Code blocks, frontmatter, and comments are left out; inline code counts. Chinese, Japanese, and Korean are counted by character, since they're written without spaces between words. `minutes` is rounded up, and 0 only for a document with no words. `countWords(text)` counts any string the same way.
23
+
24
+ It draws nothing and has no components: show the numbers in your layout.
@@ -0,0 +1,20 @@
1
+ //#region src/index.d.ts
2
+ export interface ReadingTimeOptions {
3
+ /** How many words a reader reads a minute. Default 200. */
4
+ wordsPerMinute?: number;
5
+ }
6
+ export interface ReadingTime {
7
+ /** The words in the document's text, outside code blocks. Each CJK character counts as one. */
8
+ words: number;
9
+ /** `words` over the reading speed, rounded up. 0 for a document with no words. */
10
+ minutes: number;
11
+ }
12
+ export type ReadingTimeMeta = {
13
+ readingTime: ReadingTime;
14
+ };
15
+ /** Counts the words in a text: each CJK character, plus each run of other letters and digits between spaces. */
16
+ export declare function countWords(text: string): number;
17
+ /** Writes how long a document takes to read to `meta.readingTime`, as `{ words, minutes }`, leaving code blocks out. */
18
+ declare const _default: import("comark").ComarkPluginFactory<ReadingTimeOptions, ReadingTimeMeta, {}>;
19
+ //#endregion
20
+ export { _default as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,37 @@
1
+ import { defineComarkPlugin } from "comark";
2
+ //#region src/index.ts
3
+ const CJK = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/gu;
4
+ const WORD = /[\p{L}\p{N}]/u;
5
+ const RAW_CODE = /* @__PURE__ */ new Set([
6
+ "pre",
7
+ "script",
8
+ "style",
9
+ "textarea"
10
+ ]);
11
+ /** The text of every node but code blocks, with a space between nodes so words in adjacent blocks stay apart. */
12
+ function textOf(nodes, parts = []) {
13
+ for (const node of nodes) if (!Array.isArray(node)) parts.push(node);
14
+ else if (node[0] !== null && !RAW_CODE.has(node[0].toLowerCase())) {
15
+ const [, , ...children] = node;
16
+ textOf(children, parts);
17
+ }
18
+ return parts;
19
+ }
20
+ /** Counts the words in a text: each CJK character, plus each run of other letters and digits between spaces. */
21
+ function countWords(text) {
22
+ return (text.match(CJK)?.length ?? 0) + text.replace(CJK, " ").split(/\s+/).filter((word) => WORD.test(word)).length;
23
+ }
24
+ /** Writes how long a document takes to read to `meta.readingTime`, as `{ words, minutes }`, leaving code blocks out. */
25
+ var src_default = defineComarkPlugin((options = {}) => ({
26
+ name: "comark-reading-time",
27
+ post(state) {
28
+ const speed = options.wordsPerMinute ?? 200;
29
+ const words = countWords(textOf(state.tree.nodes).join(" "));
30
+ state.tree.meta.readingTime = {
31
+ words,
32
+ minutes: Math.ceil(words / (speed > 0 ? speed : 200))
33
+ };
34
+ }
35
+ }));
36
+ //#endregion
37
+ export { countWords, src_default as default };
package/package.json CHANGED
@@ -1,12 +1,50 @@
1
1
  {
2
2
  "name": "comark-reading-time",
3
- "version": "0.0.0",
4
- "description": "Placeholder. The first release is published from https://github.com/onmax/comark-plugins.",
3
+ "version": "0.7.1",
4
+ "description": "A document's word count and reading time in meta.readingTime, with code blocks left out and CJK counted by character. A Comark plugin.",
5
+ "keywords": [
6
+ "comark",
7
+ "markdown",
8
+ "mdc",
9
+ "plugin",
10
+ "reading-time",
11
+ "word-count"
12
+ ],
13
+ "homepage": "https://comark-ui.onmax.me/reading-time",
14
+ "bugs": "https://github.com/onmax/comark-ui/issues",
5
15
  "license": "MIT",
6
16
  "author": "onmax",
7
17
  "repository": {
8
18
  "type": "git",
9
- "url": "git+https://github.com/onmax/comark-plugins.git",
19
+ "url": "git+https://github.com/onmax/comark-ui.git",
10
20
  "directory": "packages/reading-time"
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "type": "module",
26
+ "sideEffects": false,
27
+ "exports": {
28
+ ".": "./dist/index.mjs",
29
+ "./package.json": "./package.json"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public",
33
+ "provenance": true
34
+ },
35
+ "devDependencies": {
36
+ "@comark/html": "^0.8.1",
37
+ "comark": "^0.8.1"
38
+ },
39
+ "peerDependencies": {
40
+ "comark": "^0.8.0"
41
+ },
42
+ "engines": {
43
+ "node": ">=22.12.0"
44
+ },
45
+ "scripts": {
46
+ "build": "tsdown",
47
+ "dev": "tsdown --watch",
48
+ "doctor": "vite-doctor . --no-cache --max-warnings 0 --format text"
11
49
  }
12
- }
50
+ }