@ilamanownuk/blog-kit 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +148 -2
- package/dist/index.cjs +908 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +158 -0
- package/dist/index.d.ts +158 -0
- package/dist/index.js +894 -0
- package/dist/index.js.map +1 -0
- package/dist/renderer.cjs +180 -0
- package/dist/renderer.cjs.map +1 -0
- package/dist/renderer.d.cts +23 -0
- package/dist/renderer.d.ts +23 -0
- package/dist/renderer.js +142 -0
- package/dist/renderer.js.map +1 -0
- package/dist/server.cjs +214 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.d.cts +31 -0
- package/dist/server.d.ts +31 -0
- package/dist/server.js +172 -0
- package/dist/server.js.map +1 -0
- package/dist/styles.css +215 -0
- package/package.json +107 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ilamanownuk
|
|
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,149 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ilamanownuk/blog-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
ویرایشگر متن غنی Tiptap برای بلاگ (Next.js / NestJS) — درج ویدیو از **آپارات و یوتیوب**، آپلود تصویر، انتخاب از **کتابخانهی رسانه**، و sanitizer سمت سرور.
|
|
4
|
+
Reusable Tiptap blog editor with Aparat/YouTube embeds, image upload, a media-library picker, a server sanitizer and a renderer. RTL and Persian by default; no Tailwind required.
|
|
5
|
+
|
|
6
|
+
## Install / نصب
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pnpm add @ilamanownuk/blog-kit
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Peer deps: `react` and `react-dom` >= 19.
|
|
13
|
+
|
|
14
|
+
| Entry | Use |
|
|
15
|
+
| --- | --- |
|
|
16
|
+
| `@ilamanownuk/blog-kit` | `RichTextEditor` (client component, ships `"use client"`) |
|
|
17
|
+
| `@ilamanownuk/blog-kit/renderer` | `RichContent` (works in Server Components) |
|
|
18
|
+
| `@ilamanownuk/blog-kit/server` | `sanitizeRichText`, `hasRichContent`, `richTextToPlain` (no React) |
|
|
19
|
+
| `@ilamanownuk/blog-kit/styles.css` | Styles (CSS variables `--bk-*`, logical properties for RTL) |
|
|
20
|
+
|
|
21
|
+
Import the CSS once (e.g. in `app/layout.tsx`):
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import "@ilamanownuk/blog-kit/styles.css";
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Editor
|
|
28
|
+
|
|
29
|
+
```tsx
|
|
30
|
+
"use client";
|
|
31
|
+
import { RichTextEditor } from "@ilamanownuk/blog-kit";
|
|
32
|
+
import { uploadPostImage } from "./actions";
|
|
33
|
+
|
|
34
|
+
export function PostForm() {
|
|
35
|
+
const [html, setHtml] = useState("");
|
|
36
|
+
return <RichTextEditor value={html} onChange={setHtml} uploadImage={uploadPostImage} />;
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Props: `value`, `onChange(html)` (empty editor emits `""`), `placeholder`, `invalid`, `id`, `className`, `uploadImage`, `mediaApi`, `renderMediaPicker`, `resolveImageSrc`, `allowedEmbeds` (default `["aparat","youtube"]`), `labels`, `dir` (default `"rtl"`).
|
|
41
|
+
Extensions are created once on mount, so `allowedEmbeds`/`placeholder` should be stable.
|
|
42
|
+
|
|
43
|
+
### Image upload via a Next.js server action
|
|
44
|
+
|
|
45
|
+
`uploadImage(file) => Promise<{ url: string; alt?: string }>`; throwing shows the error message in the dialog. Only `https://` image URLs are accepted.
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
// actions.ts
|
|
49
|
+
"use server";
|
|
50
|
+
export async function uploadPostImage(file: File) {
|
|
51
|
+
const body = new FormData();
|
|
52
|
+
body.append("file", file);
|
|
53
|
+
const res = await fetch(`${process.env.API_URL}/media`, { method: "POST", body, headers: { authorization: `Bearer ${await getToken()}` } });
|
|
54
|
+
if (!res.ok) throw new Error("آپلود ناموفق بود");
|
|
55
|
+
const { url } = await res.json();
|
|
56
|
+
return { url };
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Media library
|
|
61
|
+
|
|
62
|
+
Built-in picker driven by a `mediaApi` (implement with server actions so tokens stay on the server):
|
|
63
|
+
|
|
64
|
+
```tsx
|
|
65
|
+
<RichTextEditor
|
|
66
|
+
value={html}
|
|
67
|
+
onChange={setHtml}
|
|
68
|
+
mediaApi={{
|
|
69
|
+
list: ({ q, page }) => listMedia({ q, page }), // -> { items, total, page, pageSize, error? }
|
|
70
|
+
upload: (formData) => uploadMedia(formData), // formData.get("file") -> { items, error? }
|
|
71
|
+
}}
|
|
72
|
+
resolveImageSrc={(url) => url.replace(/\.(\w+)$/, "-960.$1")} // optional
|
|
73
|
+
/>
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`MediaItem`: `{ id, url, originalName, mimeType, sizeBytes, width, height, alt, variants: { "320": {url,width,height} }, createdAt }`. Grid thumbnails use `variants["320"]` when present.
|
|
77
|
+
|
|
78
|
+
Or bring your own picker (takes precedence over `mediaApi`):
|
|
79
|
+
|
|
80
|
+
```tsx
|
|
81
|
+
<RichTextEditor
|
|
82
|
+
renderMediaPicker={({ onSelect, close }) => (
|
|
83
|
+
<MyPicker onPick={(m) => onSelect({ url: m.url, alt: m.alt })} onCancel={close} />
|
|
84
|
+
)}
|
|
85
|
+
...
|
|
86
|
+
/>
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Labels
|
|
90
|
+
|
|
91
|
+
All strings are Persian; override any with `labels` (see `defaultLabels` export for keys):
|
|
92
|
+
|
|
93
|
+
```tsx
|
|
94
|
+
<RichTextEditor labels={{ bold: "Bold", imageTitle: "Insert image", videoError: "Aparat or YouTube links only." }} dir="ltr" ... />
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Embeds
|
|
98
|
+
|
|
99
|
+
`allowedEmbeds={["aparat"]}` limits the toolbar, the editor parser **and** (when passed identically) the sanitizer. YouTube links are converted to `youtube-nocookie.com`.
|
|
100
|
+
|
|
101
|
+
## Server: always sanitize
|
|
102
|
+
|
|
103
|
+
Editor HTML is untrusted input. Sanitize when saving and again when rendering.
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
// Next.js server action / NestJS service
|
|
107
|
+
import { sanitizeRichText, hasRichContent, richTextToPlain } from "@ilamanownuk/blog-kit/server";
|
|
108
|
+
|
|
109
|
+
const clean = sanitizeRichText(dto.content, {
|
|
110
|
+
allowedEmbeds: ["aparat", "youtube"], // must match the editor
|
|
111
|
+
isAllowedImageSrc: (src) => src.startsWith("https://cdn.example.com/"), // optional, https is always required
|
|
112
|
+
});
|
|
113
|
+
if (!hasRichContent(clean)) throw new BadRequestException("Empty content");
|
|
114
|
+
const description = richTextToPlain(clean).slice(0, 160);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
NestJS example:
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
@Injectable()
|
|
121
|
+
export class PostsService {
|
|
122
|
+
create(dto: CreatePostDto) {
|
|
123
|
+
return this.prisma.post.create({ data: { ...dto, content: sanitizeRichText(dto.content) } });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Render
|
|
129
|
+
|
|
130
|
+
```tsx
|
|
131
|
+
import { RichContent } from "@ilamanownuk/blog-kit/renderer";
|
|
132
|
+
<RichContent html={post.content} /> // sanitizes again, dir="rtl" lang="fa"
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Styling
|
|
136
|
+
|
|
137
|
+
Override variables: `--bk-accent`, `--bk-fg`, `--bk-bg`, `--bk-border`, `--bk-radius`, `--bk-editor-min-height`, ... (see `src/styles.css`). Content uses the `.bk-rich` class.
|
|
138
|
+
|
|
139
|
+
## Updating in your projects
|
|
140
|
+
|
|
141
|
+
Bump the version (`pnpm up @ilamanownuk/blog-kit`) or let Renovate/Dependabot open the PR. Releases follow semver; the HTML allowlist lives in one place (`/server`), so editor, backend and renderer stay in sync when they share a version.
|
|
142
|
+
|
|
143
|
+
## Development
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
pnpm install && pnpm typecheck && pnpm test && pnpm build
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
MIT
|