Skip to content

Commit 28df1dd

Browse files
committed
chore: opt-in reading time
1 parent 31333d8 commit 28df1dd

8 files changed

Lines changed: 19 additions & 4 deletions

File tree

.changeset/opt-in-reading-time.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@doc-kit/generator-react': minor
3+
---
4+
5+
Make reading time opt-in via `showReadingTime`

packages/react/src/html/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,9 @@ export default ({ metadata }) => (
416416
- `metadata` {Object} Serialized page metadata — all YAML frontmatter properties
417417
plus `addedIn`, `basename`, `path`, and any custom user-defined fields.
418418
- `headings` {Array} Pre-computed table of contents heading entries.
419-
- `readingTime` {string} Estimated reading time (e.g. `'5 min read'`).
419+
- `readingTime` {string|undefined} Estimated reading time (e.g. `'5 min read'`).
420+
Only present when the `jsx-ast` generator's `showReadingTime` option is
421+
enabled.
420422
- `children` {ComponentChildren} Processed page content.
421423

422424
The `Layout` component receives the props above. Custom Layout components can use

packages/react/src/html/ui/components/Layout/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import SideBar from '#theme/Sidebar';
1818
* main content, meta bar, and footer. Override via `#theme/Layout` in your
1919
* configuration's `imports` to customize the entire page structure.
2020
*
21-
* @param {{ metadata: import('../../types').SerializedMetadata, headings: Array, readingTime: string, children: import('preact').ComponentChildren }} props
21+
* @param {{ metadata: import('../../types').SerializedMetadata, headings: Array, readingTime?: string, children: import('preact').ComponentChildren }} props
2222
*/
2323
export default ({ metadata, headings, readingTime, children }) => {
2424
const crossLinkItems = navigation.showCrossLinks

packages/react/src/html/ui/components/MetaBar/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const HeadingValue = ({ value, stability }) => {
4949

5050
/**
5151
* MetaBar component that displays table of contents and page metadata
52-
* @param {{ metadata: import('../../types').SerializedMetadata, headings: Array, readingTime: string }} props
52+
* @param {{ metadata: import('../../types').SerializedMetadata, headings: Array, readingTime?: string }} props
5353
*/
5454
export default ({ metadata, headings = [], readingTime }) => {
5555
const editThisPage = editURL?.replace('{path}', metadata.path);

packages/react/src/jsx-ast/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ The `jsx-ast` generator converts MDAST (Markdown Abstract Syntax Tree) to JSX AS
1414
for `index.html`. **Default:** `true`.
1515
- `generateNotFoundPage` {boolean} When `true`, creates a synthetic JSX AST
1616
entry for `404.html`. **Default:** `true`.
17+
- `showReadingTime` {boolean} When `true`, computes an estimated reading time
18+
for each page and displays it in the MetaBar. **Default:** `false`.

packages/react/src/jsx-ast/index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default {
1919
generateAllPage: true,
2020
generateIndexPage: true,
2121
generateNotFoundPage: true,
22+
showReadingTime: false,
2223
},
2324

2425
hasParallelProcessor: true,

packages/react/src/jsx-ast/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type Generator = GeneratorMetadata<
77
generateAllPage: boolean;
88
generateIndexPage: boolean;
99
generateNotFoundPage: boolean;
10+
showReadingTime: boolean;
1011
},
1112
Generate<Array<MetadataEntry>, AsyncGenerator<JSXContent>>,
1213
ProcessChunk<

packages/react/src/jsx-ast/utils/buildContent.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,15 @@ export const createDocumentLayout = (entries, metadata) => {
320320
// underlying headings with compact anchors / overload flags read just below.
321321
annotateOverloads(entries);
322322

323+
const { showReadingTime } = getConfig('jsx-ast');
324+
323325
return createTree('root', [
324326
createJSXElement(JSX_IMPORTS.Layout.name, {
325327
metadata,
326328
headings: extractHeadings(entries),
327-
readingTime: readingTime(extractTextContent(entries)).text,
329+
readingTime: showReadingTime
330+
? readingTime(extractTextContent(entries)).text
331+
: undefined,
328332
children: entries.map(processEntry),
329333
}),
330334
]);

0 commit comments

Comments
 (0)