AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
feat: add note character count
tianma-if70cba5f4afe7
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsxindex 45e88622..54dce634 100644--- a/apps/web/src/components/EditorPane.tsx+++ b/apps/web/src/components/EditorPane.tsx@@ -60,6 +60,7 @@ import { api } from "@/lib/api";import { consumeStandaloneMobileEditorReturn, openStandaloneMobileEditor } from "@/lib/mobile-editor";import { cn, formatDateTime, parseTagsText } from "@/lib/utils";import {+ countMemoCharacters,docToMarkdown,markdownToDoc,resolveMemoContentDoc,@@ -1106,6 +1107,7 @@ const RichEditorPane = ({const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);const [dirtyVersion, setDirtyVersion] = useState(0);const [, setEditorStateVersion] = useState(0);+ const [editorContentVersion, setEditorContentVersion] = useState(0);const [imageUploadState, setImageUploadState] = useState<"idle" | "compressing" | "uploading" | "error">("idle");const [historyOpen, setHistoryOpen] = useState(false);const [mobileNotebookSheetOpen, setMobileNotebookSheetOpen] = useState(false);@@ -1385,6 +1387,19 @@ const RichEditorPane = ({};}, [editor]);+ useEffect(() => {+ if (!isEditorReady(editor)) {+ return;+ }++ const refreshCharacterCount = () => setEditorContentVersion((version) => version + 1);+ editor.on("update", refreshCharacterCount);++ return () => {+ editor.off("update", refreshCharacterCount);+ };+ }, [editor]);+const noteSearchMatches = useMemo(() => getEditorSearchMatches(editor, noteSearchQuery),[dirtyVersion, editor, memo?.id, noteSearchQuery]@@ -1617,6 +1632,13 @@ const RichEditorPane = ({return currentEditor.getJSON() as TiptapDoc;}, [getMobilePlainTextValue, markdownSource, useMarkdownSourceEditor, useMobilePlainTextEditor]);+ const characterCount = useMemo(() => {+ const contentJson = getCurrentContentJson()+ ?? (memo ? resolveMemoContentDoc(memo.contentJson, memo.contentMarkdown) : null);++ return countMemoCharacters(contentJson);+ }, [dirtyVersion, editorContentVersion, getCurrentContentJson, memo]);+const currentSnapshot = useCallback(() => {const contentJson = getCurrentContentJson();if (!contentJson) {@@ -2356,6 +2378,12 @@ const RichEditorPane = ({</div><div className="flex shrink-0 items-center gap-1">+ <span+ className="hidden whitespace-nowrap px-1.5 text-xs tabular-nums text-slate-400 sm:inline-flex"+ title={t("editor.characterCount", { count: characterCount })}+ >+ {t("editor.characterCount", { count: characterCount })}+ </span>{imageUploadState !== "idle" && (<spanclassName={cn(diff --git a/packages/shared/src/content.test.mjs b/packages/shared/src/content.test.mjsindex 7517ecdf..6dd28a30 100644--- a/packages/shared/src/content.test.mjs+++ b/packages/shared/src/content.test.mjs@@ -1,5 +1,25 @@import { describe, expect, test } from "bun:test";-import { docToMarkdown, markdownToDoc, resolveMemoContentDoc } from "./content.ts";+import { countMemoCharacters, docToMarkdown, markdownToDoc, resolveMemoContentDoc } from "./content.ts";++describe("memo character count", () => {+ test("counts punctuation while excluding whitespace and formatting", () => {+ const doc = markdownToDoc("你好, **EdgeEver**!\n\n下一行");++ expect(countMemoCharacters(doc)).toBe(15);+ });++ test("counts grapheme clusters and ignores image labels", () => {+ const doc = {+ type: "doc",+ content: [+ { type: "paragraph", content: [{ type: "text", text: "e\u0301 👨👩👧👦" }] },+ { type: "image", attrs: { alt: "不计入" } },+ ],+ };++ expect(countMemoCharacters(doc)).toBe(2);+ });+});describe("Markdown table conversion", () => {const markdown = [diff --git a/packages/shared/src/content.ts b/packages/shared/src/content.tsindex 7d08c2ce..670c542a 100644--- a/packages/shared/src/content.ts+++ b/packages/shared/src/content.ts@@ -115,6 +115,53 @@ export const docToText = (doc: unknown): string => {return pieces.join(" ").replace(/\s+/g, " ").trim();};+let memoCharacterSegmenter: Intl.Segmenter | null | undefined;++const getMemoCharacterSegmenter = () => {+ if (memoCharacterSegmenter !== undefined) {+ return memoCharacterSegmenter;+ }++ memoCharacterSegmenter = typeof Intl.Segmenter === "function"+ ? new Intl.Segmenter(undefined, { granularity: "grapheme" })+ : null;++ return memoCharacterSegmenter;+};++/**+ * Counts visible memo-body characters while excluding whitespace. Formatting,+ * titles, tags, and image labels are intentionally not part of the count.+ */+export const countMemoCharacters = (doc: unknown): number => {+ const pieces: string[] = [];++ const walk = (node: unknown) => {+ if (!node || typeof node !== "object") {+ return;+ }++ const current = node as { text?: unknown; content?: unknown };++ if (typeof current.text === "string") {+ pieces.push(current.text);+ }++ if (Array.isArray(current.content)) {+ for (const child of current.content) {+ walk(child);+ }+ }+ };++ walk(doc);+ const text = pieces.join("");+ const segmenter = getMemoCharacterSegmenter();+ const characters = segmenter ? Array.from(segmenter.segment(text), ({ segment }) => segment) : Array.from(text);++ return characters.reduce((count, character) => count + (/^\s+$/u.test(character) ? 0 : 1), 0);+};+export const docToMarkdown = (doc: unknown): string => {if (!doc || typeof doc !== "object") {return "";diff --git a/packages/shared/src/i18n/en-US.ts b/packages/shared/src/i18n/en-US.tsindex 508a92e2..2d7d4f89 100644--- a/packages/shared/src/i18n/en-US.ts+++ b/packages/shared/src/i18n/en-US.ts@@ -493,6 +493,7 @@ export const enUS = {replaceAll: "Replace all",closeSearch: "Close search",editMemo: "Edit note",+ characterCount: "{{count}} chars",},editorToolbar: {toolbar: "Editor toolbar",diff --git a/packages/shared/src/i18n/zh-CN.ts b/packages/shared/src/i18n/zh-CN.tsindex 469157c0..ddbb79b4 100644--- a/packages/shared/src/i18n/zh-CN.ts+++ b/packages/shared/src/i18n/zh-CN.ts@@ -493,6 +493,7 @@ export const zhCN = {replaceAll: "全部替换",closeSearch: "关闭搜索",editMemo: "编辑笔记",+ characterCount: "{{count}} 字",},editorToolbar: {toolbar: "编辑器工具栏",