diff --git a/src/copyMatcher.ts b/src/copyMatcher.ts new file mode 100644 index 0000000..1470b6e --- /dev/null +++ b/src/copyMatcher.ts @@ -0,0 +1,37 @@ +import { ContextType, EasyCopySettings } from './type'; + +export type BuiltinCopyMatcherId = + | 'bold' + | 'italic' + | 'highlight' + | 'strikethrough' + | 'inline-code' + | 'inline-latex' + | 'wiki-link'; + +export interface CopyMatcher { + id: BuiltinCopyMatcherId; + type: ContextType; + regex: RegExp; + enabled: boolean; +} + +export function buildBuiltinCopyMatchers(settings: EasyCopySettings, isIosApp: boolean): CopyMatcher[] { + // iOS 16.4 之前不支持后视(Lookbehinds),但支持前视(Lookaheads) + // 所以针对 iOS 平台使用只带前视的正则表达式,其他平台使用完整版本 + const italicRegex = isIosApp ? + /(?:\*([^*]+)\*(?!\*)|_([^_]+)_(?!_))/g : + /(?:(? 斜体 > 高亮 > 删除线 > 行内代码 > 行内Latex > 块ID > 双链 - const matchers = [ - { type: ContextType.BOLD, regex: boldRegex , enable: !this.settings.customizeTargets || this.settings.enableBold}, - // 使用前后断言确保不被包裹在更长的语法中,同时支持 * 和 _ 两种格式 - { type: ContextType.ITALIC, regex: italicRegex , enable: !this.settings.customizeTargets || this.settings.enableItalic}, - { type: ContextType.HIGHLIGHT, regex: /==([^=]+)==/g , enable: !this.settings.customizeTargets || this.settings.enableHighlight}, - { type: ContextType.STRIKETHROUGH, regex: /~~([^~]+)~~/g , enable: !this.settings.customizeTargets || this.settings.enableStrikethrough}, - { type: ContextType.INLINECODE, regex: /`([^`]+)`/g , enable: !this.settings.customizeTargets || this.settings.enableInlineCode}, - { type: ContextType.INLINELATEX, regex: /\$([^$]+)\$/g , enable: !this.settings.customizeTargets || this.settings.enableInlineLatex}, - { type: ContextType.WIKILINK, regex: /\[\[([^\]]+)\]\]/g, enable: !this.settings.customizeTargets || this.settings.enableWikiLink }, - ]; + const matchers = buildBuiltinCopyMatchers(this.settings, Platform.isIosApp); for (const matcher of matchers) { - if (!matcher.enable) continue; // 如果当前类型未启用,则跳过 + if (!matcher.enabled) continue; // 如果当前类型未启用,则跳过 const matchInfo = this.getMatchInfo(beforeCursor, afterCursor, matcher.regex); if (matchInfo) { return {