From 1e80ee5bd0b85798b6cc7ba261fa6a310c539ccd Mon Sep 17 00:00:00 2001 From: tai-cha Date: Fri, 21 Feb 2025 22:29:42 +0900 Subject: [PATCH] =?UTF-8?q?hash=E3=82=92=E3=83=91=E3=82=B9=E3=81=A8?= =?UTF-8?q?=E8=A1=8C=E6=95=B0=E3=81=8B=E3=82=89=E6=B1=BA=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 - .../lib/vite-plugin-create-search-index.ts | 22 ++++-- .../src/pages/settings/mute-block.vue | 3 - .../frontend/src/pages/settings/profile.vue | 4 +- .../frontend/src/scripts/autogen/.gitkeep | 0 .../scripts/autogen/settings-search-index.ts | 76 +++++++++++++++++++ packages/frontend/vite.config.ts | 6 +- 7 files changed, 95 insertions(+), 17 deletions(-) delete mode 100644 packages/frontend/src/scripts/autogen/.gitkeep create mode 100644 packages/frontend/src/scripts/autogen/settings-search-index.ts diff --git a/.gitignore b/.gitignore index e01dfa554c..ac7502f384 100644 --- a/.gitignore +++ b/.gitignore @@ -64,7 +64,6 @@ temp /packages/frontend/src/**/*.stories.ts tsdoc-metadata.json misskey-assets -/packages/frontend/src/scripts/autogen/settings-search-index.ts # Vite temporary files vite.config.js.timestamp-* diff --git a/packages/frontend/lib/vite-plugin-create-search-index.ts b/packages/frontend/lib/vite-plugin-create-search-index.ts index 829d1f2c82..9bbc68322b 100644 --- a/packages/frontend/lib/vite-plugin-create-search-index.ts +++ b/packages/frontend/lib/vite-plugin-create-search-index.ts @@ -11,6 +11,7 @@ import JSON5 from 'json5'; import { randomUUID } from 'crypto'; import MagicString from 'magic-string'; import path from 'node:path' +import { hash, toBase62 } from '../vite.config'; export interface AnalysisResult { filePath: string; @@ -201,20 +202,26 @@ async function processVueFile( transformedCodeCache: Record ) { const s = new MagicString(code); // magic-string のインスタンスを作成 - const ast = vueSfcParse(code, { filename: id }).descriptor.template?.ast; // テンプレート AST を取得 + const parsed = vueSfcParse(code, { filename: id }); + if (!parsed.descriptor.template) { + return; + } + const ast = parsed.descriptor.template.ast; // テンプレート AST を取得 const markerRelations: MarkerRelation[] = []; // MarkerRelation 配列を初期化 if (ast) { function traverse(node: any, currentParent?: any) { - // ノードが MkSearchMarker なら、markerId を生成しノードにセット(すでに存在する場合はその値を使用) - let nodeMarkerId: string | undefined; if (node.type === 1 && node.tag === 'MkSearchMarker') { - const markerId = String(randomUUID()); + // 行番号はコード先頭からの改行数で取得 + const lineNumber = code.slice(0, node.loc.start.offset).split('\n').length; + // ファイルパスと行番号からハッシュ値を生成 + const generatedMarkerId = toBase62(hash(`${id}:${lineNumber}`)); + const props = node.props || []; const hasMarkerIdProp = props.some((prop: any) => prop.type === 6 && prop.name === 'markerId'); - nodeMarkerId = hasMarkerIdProp + const nodeMarkerId = hasMarkerIdProp ? props.find((prop: any) => prop.type === 6 && prop.name === 'markerId')?.value?.content as string - : markerId; + : generatedMarkerId; node.__markerId = nodeMarkerId; // 子マーカーの場合、親ノードに __children を設定しておく @@ -223,7 +230,6 @@ async function processVueFile( currentParent.__children.push(nodeMarkerId); } - // markerRelations の処理はそのまま const parentMarkerId = currentParent && currentParent.__markerId; markerRelations.push({ parentId: parentMarkerId, @@ -234,7 +240,7 @@ async function processVueFile( if (!hasMarkerIdProp) { const startTagEnd = code.indexOf('>', node.loc.start.offset); if (startTagEnd !== -1) { - s.appendRight(startTagEnd, ` markerId="${markerId}"`); + s.appendRight(startTagEnd, ` markerId="${generatedMarkerId}"`); } } } diff --git a/packages/frontend/src/pages/settings/mute-block.vue b/packages/frontend/src/pages/settings/mute-block.vue index 01b96766a3..71e2f89a48 100644 --- a/packages/frontend/src/pages/settings/mute-block.vue +++ b/packages/frontend/src/pages/settings/mute-block.vue @@ -6,11 +6,9 @@ SPDX-License-Identifier: AGPL-3.0-only