@posiwise/shared-components 0.0.185 → 0.0.187
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -464,9 +464,9 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.6", ngImpor
|
|
|
464
464
|
|
|
465
465
|
/**
|
|
466
466
|
* Minimal markdown-to-HTML renderer for Brain chat assistant bubbles.
|
|
467
|
-
* Handles inline links, bold, italic, and
|
|
468
|
-
* text is HTML-escaped before insertion so
|
|
469
|
-
* when consumed by DomSanitizer.bypassSecurityTrustHtml.
|
|
467
|
+
* Handles inline links, bold, italic, newlines, and bare app-relative path
|
|
468
|
+
* auto-linking. All user-supplied text is HTML-escaped before insertion so
|
|
469
|
+
* the output is injection-safe when consumed by DomSanitizer.bypassSecurityTrustHtml.
|
|
470
470
|
*
|
|
471
471
|
* Intentionally small: no full CommonMark parser, no headings, no code
|
|
472
472
|
* fences — only the subset that makes AI responses navigable.
|
|
@@ -480,6 +480,9 @@ function escapeHtml(s) {
|
|
|
480
480
|
.replace(/"/g, '"')
|
|
481
481
|
.replace(/'/g, ''');
|
|
482
482
|
}
|
|
483
|
+
/** Inline-link regex shared by linkifyBareAppPaths (matchAll) and renderMarkdownCore (LINK_ANCHORED_RE exec). */
|
|
484
|
+
const LINK_PATTERN = /\[([^\]\n]+)\]\(([^()\n]*(?:\([^()\n]*\)[^()\n]*)*)\)/g;
|
|
485
|
+
const LINK_ANCHORED_RE = new RegExp('^' + LINK_PATTERN.source);
|
|
483
486
|
/**
|
|
484
487
|
* Validate and return a safe href, or null to discard a dangerous URL.
|
|
485
488
|
* Allows app-relative paths (/foo/bar) and http/https absolute URLs.
|
|
@@ -507,6 +510,34 @@ function sanitizeMarkdownHref(url) {
|
|
|
507
510
|
}
|
|
508
511
|
return null;
|
|
509
512
|
}
|
|
513
|
+
/**
|
|
514
|
+
* Pre-process a segment of raw text (outside any existing markdown link) and
|
|
515
|
+
* convert bare app-relative paths like /account/details into markdown link
|
|
516
|
+
* syntax [/account/details](/account/details) so the main render loop picks
|
|
517
|
+
* them up as clickable anchors.
|
|
518
|
+
*
|
|
519
|
+
* Matches paths that are preceded by whitespace, certain punctuation, or the
|
|
520
|
+
* start of the string, and that begin with /letter to avoid matching lone
|
|
521
|
+
* slashes, protocol-relative URLs (//), or numeric fractions (1/2).
|
|
522
|
+
*/
|
|
523
|
+
function substituteBarePaths(segment) {
|
|
524
|
+
return segment.replace(/(^|[\s(,;:])\/([a-zA-Z][a-zA-Z0-9\-_/]*)/g, (_, pre, rest) => `${pre}[/${rest}](/${rest})`);
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* Auto-link bare app-relative paths in raw markdown text while leaving paths
|
|
528
|
+
* that are already inside a markdown link's URL segment ([text](url)) untouched.
|
|
529
|
+
*/
|
|
530
|
+
function linkifyBareAppPaths(raw) {
|
|
531
|
+
let result = '';
|
|
532
|
+
let lastIndex = 0;
|
|
533
|
+
for (const match of raw.matchAll(LINK_PATTERN)) {
|
|
534
|
+
result += substituteBarePaths(raw.slice(lastIndex, match.index));
|
|
535
|
+
result += match[0];
|
|
536
|
+
lastIndex = (match.index ?? 0) + match[0].length;
|
|
537
|
+
}
|
|
538
|
+
result += substituteBarePaths(raw.slice(lastIndex));
|
|
539
|
+
return result;
|
|
540
|
+
}
|
|
510
541
|
/**
|
|
511
542
|
* Convert a subset of Markdown to an HTML string safe for innerHTML.
|
|
512
543
|
* Processes characters sequentially so patterns never interfere with
|
|
@@ -517,8 +548,12 @@ function sanitizeMarkdownHref(url) {
|
|
|
517
548
|
* **text** → <strong>text</strong>
|
|
518
549
|
* *text* → <em>text</em>
|
|
519
550
|
* \n → <br>
|
|
551
|
+
* /path → <a href="/path">/path</a> (bare app-relative paths)
|
|
520
552
|
*/
|
|
521
553
|
function renderMarkdown(raw) {
|
|
554
|
+
return renderMarkdownCore(linkifyBareAppPaths(raw));
|
|
555
|
+
}
|
|
556
|
+
function renderMarkdownCore(raw) {
|
|
522
557
|
let out = '';
|
|
523
558
|
let buf = '';
|
|
524
559
|
let i = 0;
|
|
@@ -546,7 +581,7 @@ function renderMarkdown(raw) {
|
|
|
546
581
|
// paths like /page?q=foo(bar) or javascript:alert(1) are
|
|
547
582
|
// captured in full rather than truncating at the first ).
|
|
548
583
|
if (ch === '[') {
|
|
549
|
-
const linkMatch =
|
|
584
|
+
const linkMatch = LINK_ANCHORED_RE.exec(raw.slice(i));
|
|
550
585
|
if (linkMatch) {
|
|
551
586
|
flushBuf();
|
|
552
587
|
const text = escapeHtml(linkMatch[1]);
|
|
@@ -789,7 +824,7 @@ class PwBrainChatComponent {
|
|
|
789
824
|
return;
|
|
790
825
|
}
|
|
791
826
|
if (nav?.kind === 'rejected') {
|
|
792
|
-
this.pushAssistant('Navigation was not performed: use a single-root path (e.g. /admin/users), not //, javascript:, or data: URLs.');
|
|
827
|
+
this.pushAssistant('Navigation was not performed: use a single-root path (e.g. /admin/users), not //, javascript:, or data: URLs.', false);
|
|
793
828
|
return;
|
|
794
829
|
}
|
|
795
830
|
this.pushAssistant(response.response ?? JSON.stringify(response.tool_result ?? {}, null, 2),
|
|
@@ -802,7 +837,7 @@ class PwBrainChatComponent {
|
|
|
802
837
|
const e = err;
|
|
803
838
|
message = e.error?.detail ?? e.error?.message ?? e.message ?? message;
|
|
804
839
|
}
|
|
805
|
-
this.pushAssistant(message);
|
|
840
|
+
this.pushAssistant(message, false);
|
|
806
841
|
this.loading = false;
|
|
807
842
|
this.cdr.markForCheck();
|
|
808
843
|
}
|
|
@@ -813,12 +848,8 @@ class PwBrainChatComponent {
|
|
|
813
848
|
this.conversationId = id;
|
|
814
849
|
this.storage?.setConversationId(id);
|
|
815
850
|
}
|
|
816
|
-
pushAssistant(text, markdownEnabled =
|
|
817
|
-
this.messages.push({
|
|
818
|
-
role: 'assistant',
|
|
819
|
-
text,
|
|
820
|
-
...(markdownEnabled ? { markdownEnabled } : {})
|
|
821
|
-
});
|
|
851
|
+
pushAssistant(text, markdownEnabled = true) {
|
|
852
|
+
this.messages.push({ role: 'assistant', text, markdownEnabled });
|
|
822
853
|
this.storage?.persistMessages(this.messages);
|
|
823
854
|
this.stick(false);
|
|
824
855
|
}
|