์ง๋ ฌํ
@inkio/editor๋ ๊ธฐ๋ณธ ์ง๋ ฌํ ์ ํธ๋ฆฌํฐ๋ฅผ ์ ๊ณตํ๊ณ , ๋ฉ์
/ํด์ํ๊ทธ ์ถ์ถ์ @inkio/extension์์ ์ ๊ณตํฉ๋๋ค.
import {
toPlainText,
toSummary,
getContentStats,
} from '@inkio/editor';
import { extractMentions, extractHashtags } from '@inkio/extension';toPlainText
์๋ํฐ JSON์ ์ผ๋ฐ ํ ์คํธ๋ก ๋ณํํฉ๋๋ค.
const text = toPlainText(editorContent);
// '์ ๋ชฉ
๋ณธ๋ฌธ ๋ด์ฉ...'ํ ์คํธ ๋ ธ๋์ ํ๋ ๋ธ๋ ์ดํฌ๋ฅผ ๊ธฐ์ค์ผ๋ก ์ผ๋ฐ ํ ์คํธ๋ฅผ ์์ฑํฉ๋๋ค.
toSummary
์ฝํ ์ธ ์ ์์ฝ ํ ์คํธ๋ฅผ ์์ฑํฉ๋๋ค.
const summary = toSummary(editorContent); // ๊ธฐ๋ณธ 140์
const shortSummary = toSummary(editorContent, 80); // 80์ ์ ํ
// '์ ๋ชฉ ๋ณธ๋ฌธ ๋ด์ฉ...'maxLength๋ฅผ ์ด๊ณผํ๋ฉด ...์ด ๋ถ์ต๋๋ค.
getContentStats
์ฝํ ์ธ ํต๊ณ๋ฅผ ๋ฐํํฉ๋๋ค.
const stats = getContentStats(editorContent);
// { words: 42, chars: 256, blocks: 8 }| ํ๋ | ์ค๋ช |
|---|---|
words | ๊ณต๋ฐฑ ๊ธฐ์ค ๋จ์ด ์ |
chars | ์ด ๋ฌธ์ ์ |
blocks | ๋ธ๋ก ๋ ธ๋ ์ (paragraph, heading, list ๋ฑ) |
extractMentions
์ฝํ ์ธ ์์ ๋ฉ์ ์ ์ถ์ถํฉ๋๋ค.
const mentions = extractMentions(editorContent);
// [{ id: '1', label: 'Alice' }, { id: '2', label: 'Bob' }]์ค๋ณต์ ์๋์ผ๋ก ์ ๊ฑฐ๋ฉ๋๋ค.
extractHashtags
์ฝํ ์ธ ์์ ํด์ํ๊ทธ๋ฅผ ์ถ์ถํฉ๋๋ค.
const tags = extractHashtags(editorContent);
// ['react', 'typescript', 'frontend']# ์ ๋์ฌ๊ฐ ์ ๊ฑฐ๋ ์ํ๋ก ๋ฐํ๋ฉ๋๋ค.
ํ์ฉ ์์
๊ฒ์ ์ธ๋ฑ์ฑ
async function indexDocument(docId: string, content: JSONContent) {
const plainText = toPlainText(content);
const mentions = extractMentions(content);
const hashtags = extractHashtags(content);
await searchIndex.upsert({
id: docId,
text: plainText,
mentions: mentions.map(m => m.id),
tags: hashtags,
});
}๋ฏธ๋ฆฌ๋ณด๊ธฐ ์์ฑ
function DocumentCard({ content }: { content: JSONContent }) {
const summary = toSummary(content, 120);
const stats = getContentStats(content);
return (
<div>
<p>{summary}</p>
<span>{stats.words}๋จ์ด ยท {stats.blocks}๋ธ๋ก</span>
</div>
);
}์๋ฆผ ํ ์คํธ
function buildNotification(content: JSONContent, authorName: string) {
const mentions = extractMentions(content);
const summary = toSummary(content, 50);
return mentions.map(m => ({
userId: m.id,
message: `${authorName}๋์ด ํ์๋์ ๋ฉ์
ํ์ต๋๋ค: "${summary}"`,
}));
}Last updated on