diff --git a/apps/chrome-extension/src/scripts/copy-lark-docx-as-markdown.ts b/apps/chrome-extension/src/scripts/copy-lark-docx-as-markdown.ts index cca66d8..0ad293a 100644 --- a/apps/chrome-extension/src/scripts/copy-lark-docx-as-markdown.ts +++ b/apps/chrome-extension/src/scripts/copy-lark-docx-as-markdown.ts @@ -20,6 +20,7 @@ const enum TranslationKey { UNKNOWN_ERROR = 'unknown_error', CONTENT_LOADING = 'content_loading', NOT_SUPPORT = 'not_support', + NOT_SUPPORT_DOC_1_0 = 'not_support_doc_1_0', } i18next @@ -34,6 +35,8 @@ i18next 'Part of the content is still loading and cannot be copied at the moment. Please wait for loading to complete and retry', [TranslationKey.NOT_SUPPORT]: 'This is not a lark document page and cannot be copied as Markdown', + [TranslationKey.NOT_SUPPORT_DOC_1_0]: + 'This is a old version lark document page and cannot be copied as Markdown', }, ...en, }, @@ -45,6 +48,8 @@ i18next '部分内容仍在加载中,暂时无法复制。请等待加载完成后重试', [TranslationKey.NOT_SUPPORT]: '这不是一个飞书文档页面,无法复制为 Markdown', + [TranslationKey.NOT_SUPPORT_DOC_1_0]: + '这是一个旧版飞书文档页面,无法复制为 Markdown', }, ...zh, }, @@ -53,7 +58,13 @@ i18next .catch(console.error) const main = async () => { - if (!docx.rootBlock) { + if (docx.isDoc) { + Toast.warning({ content: i18next.t(TranslationKey.NOT_SUPPORT_DOC_1_0) }) + + return + } + + if (!docx.isDocx) { Toast.warning({ content: i18next.t(TranslationKey.NOT_SUPPORT) }) return diff --git a/apps/chrome-extension/src/scripts/download-lark-docx-as-markdown.ts b/apps/chrome-extension/src/scripts/download-lark-docx-as-markdown.ts index 42bf02f..3498200 100644 --- a/apps/chrome-extension/src/scripts/download-lark-docx-as-markdown.ts +++ b/apps/chrome-extension/src/scripts/download-lark-docx-as-markdown.ts @@ -26,6 +26,7 @@ const enum TranslationKey { CONTENT_LOADING = 'content_loading', UNKNOWN_ERROR = 'unknown_error', NOT_SUPPORT = 'not_support', + NOT_SUPPORT_DOC_1_0 = 'not_support_doc_1_0', DOWNLOADING_FILE = 'downloading_file', FAILED_TO_DOWNLOAD = 'failed_to_download', DOWNLOAD_PROGRESS = 'download_progress', @@ -53,6 +54,8 @@ i18next [TranslationKey.UNKNOWN_ERROR]: 'Unknown error during download', [TranslationKey.NOT_SUPPORT]: 'This is not a lark document page and cannot be downloaded as Markdown', + [TranslationKey.NOT_SUPPORT_DOC_1_0]: + 'This is a old version lark document page and cannot be downloaded as Markdown', [TranslationKey.DOWNLOADING_FILE]: 'Download {{name}} in: {{progress}}% (please do not refresh or close the page)', [TranslationKey.FAILED_TO_DOWNLOAD]: 'Failed to download {{name}}', @@ -75,6 +78,8 @@ i18next [TranslationKey.UNKNOWN_ERROR]: '下载过程中出现未知错误', [TranslationKey.NOT_SUPPORT]: '这不是一个飞书文档页面,无法下载为 Markdown', + [TranslationKey.NOT_SUPPORT_DOC_1_0]: + '这是一个旧版飞书文档页面,无法下载为 Markdown', [TranslationKey.DOWNLOADING_FILE]: '下载 {{name}} 中:{{progress}}%(请不要刷新或关闭页面)', [TranslationKey.FAILED_TO_DOWNLOAD]: '下载 {{name}} 失败', @@ -516,7 +521,13 @@ const prepare = async (): Promise => { const main = async (options: { signal?: AbortSignal } = {}) => { const { signal } = options - if (!docx.rootBlock) { + if (docx.isDoc) { + Toast.warning({ content: i18next.t(TranslationKey.NOT_SUPPORT_DOC_1_0) }) + + throw new Error(DOWNLOAD_ABORTED) + } + + if (!docx.isDocx) { Toast.warning({ content: i18next.t(TranslationKey.NOT_SUPPORT) }) throw new Error(DOWNLOAD_ABORTED) diff --git a/apps/chrome-extension/src/scripts/view-lark-docx-as-markdown.ts b/apps/chrome-extension/src/scripts/view-lark-docx-as-markdown.ts index 0ebcb0c..6ed618c 100644 --- a/apps/chrome-extension/src/scripts/view-lark-docx-as-markdown.ts +++ b/apps/chrome-extension/src/scripts/view-lark-docx-as-markdown.ts @@ -20,6 +20,7 @@ const enum TranslationKey { UNKNOWN_ERROR = 'unknown_error', CONTENT_LOADING = 'content_loading', NOT_SUPPORT = 'not_support', + NOT_SUPPORT_DOC_1_0 = 'not_support_doc_1_0', FAILED_TO_OPEN_WINDOW = 'failed_to_open_window', } @@ -35,6 +36,8 @@ i18next 'Part of the content is still loading and cannot be copied at the moment. Please wait for loading to complete and retry', [TranslationKey.NOT_SUPPORT]: 'This is not a lark document page and cannot be copied as Markdown', + [TranslationKey.NOT_SUPPORT_DOC_1_0]: + 'This is a old version lark document page and cannot be copied as Markdown', [TranslationKey.FAILED_TO_OPEN_WINDOW]: 'Failed to Open a new window to display markdown.', }, @@ -48,6 +51,8 @@ i18next '部分内容仍在加载中,暂时无法复制。请等待加载完成后重试', [TranslationKey.NOT_SUPPORT]: '这不是一个飞书文档页面,无法复制为 Markdown', + [TranslationKey.NOT_SUPPORT_DOC_1_0]: + '这是一个旧版飞书文档页面,无法复制为 Markdown', [TranslationKey.FAILED_TO_OPEN_WINDOW]: '无法打开新窗口以显示 Markdown', }, @@ -58,8 +63,15 @@ i18next .catch(console.error) const main = async () => { - if (!docx.rootBlock) { + if (docx.isDoc) { + Toast.warning({ content: i18next.t(TranslationKey.NOT_SUPPORT_DOC_1_0) }) + + return + } + + if (!docx.isDocx) { Toast.warning({ content: i18next.t(TranslationKey.NOT_SUPPORT) }) + return } @@ -67,6 +79,7 @@ const main = async () => { Toast.warning({ content: i18next.t(TranslationKey.CONTENT_LOADING), }) + return } diff --git a/packages/lark/src/docx.ts b/packages/lark/src/docx.ts index 092cee7..216f505 100644 --- a/packages/lark/src/docx.ts +++ b/packages/lark/src/docx.ts @@ -14,7 +14,7 @@ import { gfmStrikethroughToMarkdown } from 'mdast-util-gfm-strikethrough' import { gfmTaskListItemToMarkdown } from 'mdast-util-gfm-task-list-item' import { gfmTableToMarkdown } from 'mdast-util-gfm-table' import { mathToMarkdown, type InlineMath } from 'mdast-util-math' -import { PageMain, User } from './env' +import { PageMain, User, isDoc, isDocx } from './env' import { isBlockquoteContent, isParent, @@ -1547,6 +1547,14 @@ export class Docx { return false } + get isDocx(): boolean { + return isDocx() + } + + get isDoc(): boolean { + return !isDocx() && isDoc() + } + get rootBlock(): PageBlock | null { if (!PageMain) { return null diff --git a/packages/lark/src/env.ts b/packages/lark/src/env.ts index 498c69d..3fcc876 100644 --- a/packages/lark/src/env.ts +++ b/packages/lark/src/env.ts @@ -56,3 +56,6 @@ export interface PageMain { } export const PageMain: PageMain | undefined = window.PageMain + +export const isDoc = (): boolean => window.editor !== undefined +export const isDocx = (): boolean => window.PageMain !== undefined diff --git a/packages/lark/src/lark-env.d.ts b/packages/lark/src/lark-env.d.ts index ac2ee29..7658d25 100644 --- a/packages/lark/src/lark-env.d.ts +++ b/packages/lark/src/lark-env.d.ts @@ -1,4 +1,5 @@ interface Window { + editor?: Object PageMain?: import('./env').PageMain Toast?: import('./env').Toast User?: import('./env').User