Skip to content

armgit / Orkas

Public Imported from GitHub
0

Orkas is an open-source (MIT), local-first AI team for your desktop: a Commander turns your goal into to-dos and coordinates 16 built-in specialist agents plus Claude Code, Codex, OpenCode, OpenClaw and Hermes, with projects, scheduled automations and connectors. Free app for macOS & Windows; built-in models or your own keys.

Treat CJK characters as bare-URL terminators in autolink regex

wyt464aab85eccb
diff --git a/src/renderer/modules/utils.js b/src/renderer/modules/utils.js
index 7c592527..f8c14cf8 100644
--- a/src/renderer/modules/utils.js
+++ b/src/renderer/modules/utils.js
@@ -307,8 +307,59 @@ function _isVideoSrc(src) {
return _VIDEO_EXT_RE.test(String(src || ''));
}
+// Bare URL autolink termination set. URLs per RFC 3986 are ASCII; CJK
+// ideographs / kana / hangul / CJK punctuation never appear in a real URL
+// (IRIs encode the host as punycode and the path as percent-encoded UTF-8).
+// Without these ranges in the regex's negated char class, a URL embedded
+// in a CJK sentence is matched through the trailing CJK run and the link
+// visually swallows the prose. Reported case: a fullwidth comma after a
+// URL pulled the rest of the Chinese sentence into the anchor.
+const _URL_NON_TERMINATOR =
+ '\u4e00-\u9fff' + // CJK Unified Ideographs
+ '\u3000-\u303f' + // CJK Symbols and Punctuation
+ '\uff00-\uffef' + // Halfwidth and Fullwidth Forms (incl. fullwidth ASCII punct)
+ '\u3040-\u309f' + // Hiragana
+ '\u30a0-\u30ff' + // Katakana
+ '\uac00-\ud7af'; // Hangul Syllables
+
+// Bare URL autolink. Negative lookbehind keeps us out of:
+// 1) URLs already inside an HTML attr (preceded by `"` / `'` / `=`) from
+// earlier phases (markdown links / image src / `<url>` autolinks) —
+// must NOT double-wrap;
+// 2) mid-string positions (preceded by URL-internal chars) that look
+// like the tail of a longer URL.
+// Termination set excludes ASCII URL-incompatible chars + the CJK ranges
+// so the URL ends at the first non-URL boundary.
+const _BARE_URL_RE = new RegExp(
+ '(?<![a-zA-Z0-9._\\-:/?=&#%+"\'>])' +
+ '(https?:\\/\\/[^\\s<>"\'`)\\]' + _URL_NON_TERMINATOR + ']+)',
+ 'g'
+);
+
+const _BARE_EMAIL_RE = new RegExp(
+ '(?<![a-zA-Z0-9._\\-:/?=&#%+"\'>])' +
+ '([\\w.+-]+@[\\w.-]+\\.[A-Za-z]{2,})',
+ 'g'
+);
+
+// Replace bare http(s) URLs with `<a>` tags. Trailing ASCII sentence punct
+// (`.,;:!?)`) is trimmed off the link and emitted as plain text after it,
+// so "see https://x.com." renders with the period outside the link.
+function _linkifyBareUrls(text) {
+ return text.replace(_BARE_URL_RE, (_, url) => {
+ const trail = (url.match(/[.,;:!?)]+$/) || [''])[0];
+ const clean = trail ? url.slice(0, -trail.length) : url;
+ return `<a href="${clean}" target="_blank" rel="noopener">${clean}</a>${trail}`;
+ });
+}
+
+function _linkifyBareEmails(text) {
+ return text.replace(_BARE_EMAIL_RE, (_, email) => `<a href="mailto:${email}">${email}</a>`);
+}
+
function inlineFormat(text) {
- return text
+ // Phase 1: media + markdown links + `<url>` / `<email>` autolinks + emphasis.
+ const phase1 = text
// Media: ![alt](src) — dispatch to <video> when src looks like a video
// file, else <img>. Must run before link syntax.
.replace(/!\[([^\]]*)\]\(([^)\s]+)(?:\s+"([^"]*)")?\)/g,
@@ -333,28 +384,12 @@ function inlineFormat(text) {
// Emphasis
.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
.replace(/\*([^*]+)\*/g, '<em>$1</em>')
- .replace(/~~([^~]+)~~/g, '<del>$1</del>')
- // Bare URL autolink — negative lookbehind keeps us out of two cases:
- // 1) the URL is already inside an HTML attr (preceded by `"`/`'`/`=`)
- // from earlier phases (markdown links, image src, etc.) — must NOT
- // double-wrap;
- // 2) the URL is mid-string (preceded by URL-internal chars) — looks
- // like the tail of a longer URL, skip to avoid splicing.
- // Everything else (CJK punctuation like `:`/`,`/`(`, ASCII letters,
- // line start) is allowed — the old `[\s([]` whitelist missed CJK
- // punctuation which is the most common case in Chinese chat output.
- .replace(/(?<![a-zA-Z0-9._\-:\/?=&#%+"'>])(https?:\/\/[^\s<>"'`)\]]+)/g, (_, url) => {
- const trail = (url.match(/[.,;:!?)]+$/) || [''])[0];
- const clean = trail ? url.slice(0, -trail.length) : url;
- return `<a href="${clean}" target="_blank" rel="noopener">${clean}</a>${trail}`;
- })
- // Bare email autolink — uses the same exclusion set as bare URL so that
- // an email already wrapped in an earlier `<a href="mailto:...">` (where
- // the address is preceded by `:`) doesn't get re-wrapped, and that an
- // address sitting inside a URL query like `[email protected]` (preceded by
- // `=`) is left alone too.
- .replace(/(?<![a-zA-Z0-9._\-:\/?=&#%+"'>])([\w.+-]+@[\w.-]+\.[A-Za-z]{2,})/g,
- (_, email) => `<a href="mailto:${email}">${email}</a>`);
+ .replace(/~~([^~]+)~~/g, '<del>$1</del>');
+ // Phase 2: bare URL + bare email autolinks. Split out so set-A
+ // (real URLs in CJK / ASCII contexts must end at the right boundary) and
+ // set-B (URLs inside attrs / already-wrapped links must not be re-wrapped)
+ // are pinned by `test/renderer/utils-autolink.test.ts`.
+ return _linkifyBareEmails(_linkifyBareUrls(phase1));
}
// Unified entrypoint: all chat bubbles, skill detail pages, streaming finals
@@ -370,7 +405,9 @@ const renderMarkdown = renderMarkdownFull;
// setWindowOpenHandler / will-navigate (see main/index.ts) is the safety net
// for clicks that arrive before this script evaluates or when window.orkas
// hasn't been wired yet (then we don't preventDefault and let main handle it).
-document.addEventListener('click', (e) => {
+// Guarded for Node test env where `document` is undefined; the click router
+// is a renderer-only side effect and irrelevant to the autolink fixtures.
+if (typeof document !== 'undefined') document.addEventListener('click', (e) => {
const a = e.target && e.target.closest && e.target.closest('a[href]');
if (!a) return;
const href = a.getAttribute('href');
@@ -649,3 +686,18 @@ function _aiSelectPick(api, value) {
}
}
+// Test bridge — guarded CommonJS export of the pure helpers used by the
+// markdown autolink phase. No-op in the browser (`module` undefined). Per
+// PC/CLAUDE.md §9 only pure functions go through this bridge; the rest of
+// utils.js (DOM-coupled helpers like `_aiSelectMount`) stays unexported.
+// Matching test file: `test/renderer/utils-autolink.test.ts`.
+if (typeof module !== 'undefined' && typeof module.exports === 'object') {
+ module.exports = {
+ _BARE_URL_RE,
+ _BARE_EMAIL_RE,
+ _linkifyBareUrls,
+ _linkifyBareEmails,
+ inlineFormat,
+ escapeHtml,
+ };
+}
diff --git a/test/renderer/utils-autolink.test.ts b/test/renderer/utils-autolink.test.ts
new file mode 100644
index 00000000..286f5c0e
--- /dev/null
+++ b/test/renderer/utils-autolink.test.ts
@@ -0,0 +1,176 @@
+// Pin the bare URL autolink behavior in `src/renderer/modules/utils.js`.
+//
+// Set A — URLs that MUST end at the right boundary:
+// ASCII whitespace, fullwidth punctuation (comma / period / colon /
+// semicolon / question / exclamation / paren), CJK ideographs, kana,
+// hangul. The reported bug shipped a Chinese full-width comma right
+// after a URL pulling the rest of the sentence into the anchor; the
+// matrix below covers that shape and its siblings.
+// Set B — literal mentions that must NOT be re-wrapped: URLs already
+// inside an `<a>` href or wrapped from earlier markdown phases. The
+// negative lookbehind prevents the bare-URL pass from double-wrapping.
+//
+// Adding a guard / branch / extra char-class tweak to `_BARE_URL_RE`?
+// Per PC/CLAUDE.md §9: extend this fixture set with the motivating
+// shape AND keep the existing fixtures green. The previous form of this
+// regex shipped without test coverage and lasted years before the CJK
+// case was reported; that gap is what this file closes.
+
+import { describe, it, expect } from 'vitest';
+// eslint-disable-next-line @typescript-eslint/no-var-requires
+const utils = require('../../src/renderer/modules/utils.js');
+const {
+ _BARE_URL_RE,
+ _linkifyBareUrls,
+ inlineFormat,
+} = utils as {
+ _BARE_URL_RE: RegExp;
+ _linkifyBareUrls: (text: string) => string;
+ inlineFormat: (text: string) => string;
+};
+
+const A = (url: string) =>
+ `<a href="${url}" target="_blank" rel="noopener">${url}</a>`;
+
+// --- Set A: URLs must end at the correct boundary ------------------------
+
+describe('set A — bare URL termination boundary', () => {
+ it('A1. ASCII whitespace (baseline)', () => {
+ expect(_linkifyBareUrls('see https://x.com here'))
+ .toBe(`see ${A('https://x.com')} here`);
+ });
+
+ it('A2. fullwidth comma (the reported bug)', () => {
+ const buf = 'see https://skills.sh/,然后参考';
+ const out = _linkifyBareUrls(buf);
+ expect(out).toBe(`see ${A('https://skills.sh/')},然后参考`);
+ });
+
+ it('A3. CJK ideographic period', () => {
+ const buf = 'see https://x.com。末尾';
+ expect(_linkifyBareUrls(buf)).toBe(`see ${A('https://x.com')}。末尾`);
+ });
+
+ it('A4. fullwidth colon / semicolon / question / exclamation', () => {
+ for (const punct of [':', ';', '?', '!']) {
+ const buf = `see https://x.com${punct}rest`;
+ expect(_linkifyBareUrls(buf)).toBe(`see ${A('https://x.com')}${punct}rest`);
+ }
+ });
+
+ it('A5. fullwidth parens', () => {
+ expect(_linkifyBareUrls('see https://x.com(note'))
+ .toBe(`see ${A('https://x.com')}(note`);
+ expect(_linkifyBareUrls('see https://x.com)'))
+ .toBe(`see ${A('https://x.com')})`);
+ });
+
+ it('A6. URL preceded by fullwidth colon (Chinese sentence start)', () => {
+ const buf = '可以参考:https://x.com。';
+ expect(_linkifyBareUrls(buf))
+ .toBe(`可以参考:${A('https://x.com')}。`);
+ });
+
+ it('A7. URL ending at hiragana', () => {
+ const buf = 'https://x.comひ';
+ expect(_linkifyBareUrls(buf)).toBe(`${A('https://x.com')}ひ`);
+ });
+
+ it('A8. URL ending at katakana', () => {
+ const buf = 'https://x.comカ';
+ expect(_linkifyBareUrls(buf)).toBe(`${A('https://x.com')}カ`);
+ });
+
+ it('A9. URL ending at hangul', () => {
+ const buf = 'https://x.com가';
+ expect(_linkifyBareUrls(buf)).toBe(`${A('https://x.com')}가`);
+ });
+
+ it('A10. URL ending at CJK ideograph (no punctuation)', () => {
+ const buf = 'https://x.com中文';
+ expect(_linkifyBareUrls(buf)).toBe(`${A('https://x.com')}中文`);
+ });
+
+ it('A11. trailing ASCII period — period outside the link', () => {
+ expect(_linkifyBareUrls('see https://x.com.'))
+ .toBe(`see ${A('https://x.com')}.`);
+ });
+
+ it('A12. URL with query string and fragment', () => {
+ const url = 'https://x.com/path?q=v&k=1#frag';
+ expect(_linkifyBareUrls(`go ${url}`)).toBe(`go ${A(url)}`);
+ });
+
+ it('A13. URL on its own line — full URL captured', () => {
+ expect(_linkifyBareUrls('https://skillhub.cn/skills/find-skills'))
+ .toBe(A('https://skillhub.cn/skills/find-skills'));
+ });
+
+ it('A14. multiple URLs in one CJK sentence', () => {
+ const buf = '参考 https://a.com/,还有 https://b.com/。';
+ const out = _linkifyBareUrls(buf);
+ expect(out).toContain(A('https://a.com/'));
+ expect(out).toContain(A('https://b.com/'));
+ expect(out).not.toContain('href="https://a.com/,');
+ expect(out).not.toContain('href="https://b.com/。');
+ });
+});
+
+// --- Set B: must not double-wrap or re-wrap ------------------------------
+
+describe('set B — already-wrapped URLs must not be re-wrapped', () => {
+ it('B1. URL inside `<a href="...">` is left alone (lookbehind catches `"`)', () => {
+ const wrapped = `<a href="https://x.com">https://x.com</a>`;
+ expect(_linkifyBareUrls(wrapped)).toBe(wrapped);
+ });
+
+ it('B2. markdown `[text](url)` round-trips through inlineFormat without re-wrap', () => {
+ const out = inlineFormat('[click](https://x.com)');
+ // exactly one <a> tag
+ expect((out.match(/<a /g) || []).length).toBe(1);
+ expect(out).toContain('href="https://x.com"');
+ });
+
+ it('B3. `<https://x.com>` autolink round-trips without re-wrap', () => {
+ const out = inlineFormat('<https://x.com>');
+ expect((out.match(/<a /g) || []).length).toBe(1);
+ });
+
+ it('B4. URL inside an existing img src attr is not re-matched', () => {
+ const wrapped = `<img src="https://x.com/img.png" alt="">`;
+ expect(_linkifyBareUrls(wrapped)).toBe(wrapped);
+ });
+});
+
+// --- Boundary regex sanity ----------------------------------------------
+
+describe('_BARE_URL_RE termination set', () => {
+ it('rejects fullwidth comma as URL char', () => {
+ // , (fullwidth comma) must NOT be captured as part of URL body
+ const m = 'see https://x.com,more'.match(_BARE_URL_RE);
+ expect(m && m[0]).toBe('https://x.com');
+ });
+
+ it('rejects CJK ideograph as URL char', () => {
+ const m = 'see https://x.com中more'.match(_BARE_URL_RE);
+ expect(m && m[0]).toBe('https://x.com');
+ });
+
+ it('still accepts ASCII URL chars including `?`, `=`, `&`, `#`, `%`, `_`', () => {
+ const m = 'go https://x.com/path?q=v&k=1#a_b%20c here'.match(_BARE_URL_RE);
+ expect(m && m[0]).toBe('https://x.com/path?q=v&k=1#a_b%20c');
+ });
+});
+
+// --- Empty / no-op inputs -----------------------------------------------
+
+describe('empty / no-op inputs', () => {
+ it('empty string passes through', () => {
+ expect(_linkifyBareUrls('')).toBe('');
+ });
+
+ it('plain prose without URL passes through', () => {
+ const buf = '今天天气不错';
+ expect(_linkifyBareUrls(buf)).toBe(buf);
+ });
+});