@asteroidcms/core-utils 0.1.0 → 0.1.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/README.md +43 -15
- package/dist/index.cjs +30 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +33 -1
- package/dist/index.d.ts +33 -1
- package/dist/index.js +30 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
<span>/core-utils</span>
|
|
6
6
|
</div>
|
|
7
7
|
</h1>
|
|
8
|
-
<p>Seamless integration utilities for <a href="https://cms.theasteroid.tech">Asteroid CMS</a>
|
|
8
|
+
<p>Seamless integration utilities for <a href="https://cms.theasteroid.tech">Asteroid CMS</a> - a single React provider, Apollo client, content hooks, media helpers, and a rich-text renderer.</p>
|
|
9
9
|
</div>
|
|
10
10
|
|
|
11
|
-
- **Provider-driven**
|
|
12
|
-
- **API-key auth only**
|
|
13
|
-
- **Typed hooks**
|
|
14
|
-
- **Tree-shakeable**
|
|
11
|
+
- **Provider-driven** - configure `cmsUrl`, `apiKey`, and Apollo behavior in one place
|
|
12
|
+
- **API-key auth only** - sends `x-api-key` on every request, nothing else
|
|
13
|
+
- **Typed hooks** - `useCmsContent` / `useCmsMutate` build GraphQL on the fly from a declarative selection
|
|
14
|
+
- **Tree-shakeable** - ESM + CJS + types, `@apollo/client`/`react` as peer deps
|
|
15
15
|
|
|
16
16
|
---
|
|
17
17
|
|
|
@@ -82,15 +82,15 @@ function NewsList() {
|
|
|
82
82
|
|
|
83
83
|
| Prop | Type | Required | Default | Description |
|
|
84
84
|
| --------------- | ------------------------------ | -------- | ------------------ | ------------------------------------------------------------ |
|
|
85
|
-
| `cmsUrl` | `string` | ✓ |
|
|
86
|
-
| `apiKey` | `string` | ✓ |
|
|
85
|
+
| `cmsUrl` | `string` | ✓ | - | Base URL of the Asteroid CMS API. |
|
|
86
|
+
| `apiKey` | `string` | ✓ | - | Sent on every request as the `x-api-key` header. |
|
|
87
87
|
| `graphqlPath` | `string` | | `/graphql` | Path appended to `cmsUrl` for the GraphQL endpoint. |
|
|
88
88
|
| `mediaPath` | `string` | | `/media/canonical` | Path used by `cmsImage` / `useCmsImage`. |
|
|
89
89
|
| `headers` | `Record<string, string>` | | `{}` | Extra headers merged onto every GraphQL request. |
|
|
90
|
-
| `onError` | `(error: unknown) => void` | |
|
|
91
|
-
| `cacheConfig` | `InMemoryCacheConfig` | |
|
|
92
|
-
| `apolloOptions` | `Partial<ApolloClientOptions>` | |
|
|
93
|
-
| `client` | `ApolloClient` | |
|
|
90
|
+
| `onError` | `(error: unknown) => void` | | - | Called for each GraphQL / network / protocol error. |
|
|
91
|
+
| `cacheConfig` | `InMemoryCacheConfig` | | - | Forwarded to `new InMemoryCache(...)` - e.g. `typePolicies`. |
|
|
92
|
+
| `apolloOptions` | `Partial<ApolloClientOptions>` | | - | Escape hatch - overrides any field on the Apollo client. |
|
|
93
|
+
| `client` | `ApolloClient` | | - | Bring your own pre-built client; skips the internal factory. |
|
|
94
94
|
|
|
95
95
|
Example with everything wired:
|
|
96
96
|
|
|
@@ -244,7 +244,7 @@ removeComment();
|
|
|
244
244
|
Build a canonical media URL for an asset id.
|
|
245
245
|
|
|
246
246
|
```tsx
|
|
247
|
-
// Inside React
|
|
247
|
+
// Inside React - preferred
|
|
248
248
|
const cmsImage = useCmsImage();
|
|
249
249
|
<img src={cmsImage(article.cover_image)} alt="" />;
|
|
250
250
|
|
|
@@ -255,6 +255,34 @@ cmsImage(id, { cmsUrl: "https://cms-api.example.com" });
|
|
|
255
255
|
|
|
256
256
|
---
|
|
257
257
|
|
|
258
|
+
## `getContentReadTime`
|
|
259
|
+
|
|
260
|
+
Estimate reading time for a string of content (plain text or HTML). Strips tags, decodes common entities, counts words, and formats the result.
|
|
261
|
+
|
|
262
|
+
```ts
|
|
263
|
+
import { getContentReadTime } from "@asteroidcms/core-utils";
|
|
264
|
+
|
|
265
|
+
getContentReadTime(article.body);
|
|
266
|
+
// "3 min read"
|
|
267
|
+
|
|
268
|
+
getContentReadTime(article.body, {
|
|
269
|
+
wordsPerMinute: 220,
|
|
270
|
+
format: "long",
|
|
271
|
+
round: "round",
|
|
272
|
+
minMinutes: 1,
|
|
273
|
+
});
|
|
274
|
+
// "3 minutes read"
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
| Option | Type | Default | Description |
|
|
278
|
+
| ---------------- | ------------------------------- | --------- | ------------------------------------------------- |
|
|
279
|
+
| `wordsPerMinute` | `number` | `200` | Average reading speed. |
|
|
280
|
+
| `format` | `"short" \| "long"` | `"short"` | `"3 min read"` vs `"3 minutes read"`. |
|
|
281
|
+
| `round` | `"ceil" \| "round" \| "floor"` | `"ceil"` | How fractional minutes are rounded. |
|
|
282
|
+
| `minMinutes` | `number` | `1` | Floor for the returned value. |
|
|
283
|
+
|
|
284
|
+
---
|
|
285
|
+
|
|
258
286
|
## `<RichTextContent>`
|
|
259
287
|
|
|
260
288
|
Render Asteroid CMS rich-text JSON/HTML with syntax-highlighted code blocks (via `highlight.js`).
|
|
@@ -282,7 +310,7 @@ const html = parseRichText(article.body, {
|
|
|
282
310
|
|
|
283
311
|
---
|
|
284
312
|
|
|
285
|
-
## Advanced
|
|
313
|
+
## Advanced - bring your own Apollo client
|
|
286
314
|
|
|
287
315
|
```tsx
|
|
288
316
|
import { ApolloClient, InMemoryCache } from "@apollo/client";
|
|
@@ -320,8 +348,8 @@ npm run build # writes dist/index.js, dist/index.cjs, dist/index.d.ts
|
|
|
320
348
|
|
|
321
349
|
## License
|
|
322
350
|
|
|
323
|
-
Proprietary
|
|
351
|
+
Proprietary - Copyright © Asteroid. All rights reserved.
|
|
324
352
|
|
|
325
353
|
This package is licensed for use only; copying, modifying, or
|
|
326
|
-
redistributing the source
|
|
354
|
+
redistributing the source - in whole or in part - is not permitted.
|
|
327
355
|
See [LICENSE](./LICENSE) for the full terms.
|
package/dist/index.cjs
CHANGED
|
@@ -324,6 +324,35 @@ function useCmsImage() {
|
|
|
324
324
|
return (id) => cmsImage(id, { cmsUrl, mediaPath });
|
|
325
325
|
}
|
|
326
326
|
|
|
327
|
+
// src/utils/getContentReadTime.ts
|
|
328
|
+
function getContentReadTime(content, options = {}) {
|
|
329
|
+
const {
|
|
330
|
+
wordsPerMinute = 200,
|
|
331
|
+
format = "short",
|
|
332
|
+
round = "ceil",
|
|
333
|
+
minMinutes = 1
|
|
334
|
+
} = options;
|
|
335
|
+
if (!content || typeof content !== "string") {
|
|
336
|
+
return format === "short" ? `${minMinutes} min read` : `${minMinutes} minute read`;
|
|
337
|
+
}
|
|
338
|
+
let text = content.replace(/<script[\s\S]*?<\/script>/gi, " ").replace(/<style[\s\S]*?<\/style>/gi, " ");
|
|
339
|
+
text = text.replace(/<\/(p|div|h[1-6]|li|blockquote|pre|code|br|tr)>/gi, " ").replace(/<[^>]+>/g, " ");
|
|
340
|
+
text = text.replace(/ /gi, " ").replace(/&/gi, "&").replace(/</gi, "<").replace(/>/gi, ">").replace(/'/gi, "'").replace(/"/gi, '"');
|
|
341
|
+
text = text.replace(/\s+/g, " ").trim();
|
|
342
|
+
const words = text.split(" ").filter(Boolean).length;
|
|
343
|
+
const rawMinutes = words / wordsPerMinute;
|
|
344
|
+
const roundMap = {
|
|
345
|
+
ceil: Math.ceil,
|
|
346
|
+
round: Math.round,
|
|
347
|
+
floor: Math.floor
|
|
348
|
+
};
|
|
349
|
+
const minutes = Math.max(minMinutes, roundMap[round](rawMinutes));
|
|
350
|
+
if (format === "long") {
|
|
351
|
+
return `${minutes} ${minutes === 1 ? "minute" : "minutes"} read`;
|
|
352
|
+
}
|
|
353
|
+
return `${minutes} min read`;
|
|
354
|
+
}
|
|
355
|
+
|
|
327
356
|
// src/components/richTextParser.ts
|
|
328
357
|
var DEFAULT_ALLOWLIST = [
|
|
329
358
|
"p",
|
|
@@ -1460,6 +1489,7 @@ exports.AsteroidCMSProvider = AsteroidCMSProvider;
|
|
|
1460
1489
|
exports.RichTextContent = RichTextContent;
|
|
1461
1490
|
exports.cmsImage = cmsImage;
|
|
1462
1491
|
exports.createApolloClient = createApolloClient;
|
|
1492
|
+
exports.getContentReadTime = getContentReadTime;
|
|
1463
1493
|
exports.parseRichText = parseRichText;
|
|
1464
1494
|
exports.removeEmptyParagraphs = removeEmptyParagraphs;
|
|
1465
1495
|
exports.useAsteroidCMSConfig = useAsteroidCMSConfig;
|