Skip to content

Commit

Permalink
feat: add target ccallback for markdown links
Browse files Browse the repository at this point in the history
  • Loading branch information
rjborba committed Nov 25, 2024
1 parent 359a9ba commit d78268a
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 73 deletions.
8 changes: 7 additions & 1 deletion packages/ui-stencil-vue/lib/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ export const OramaChat = /*@__PURE__*/ defineContainer<JSX.OramaChat>('orama-cha
'systemPrompts',
'chatMarkdownLinkTitle',
'chatMarkdownLinkHref',
'chatMarkdownLinkTarget',
'answerGenerated'
]);


export const OramaChatAssistentMessage = /*@__PURE__*/ defineContainer<JSX.OramaChatAssistentMessage>('orama-chat-assistent-message', undefined, [
'interaction',
'chatMarkdownLinkTitle',
'chatMarkdownLinkHref'
'chatMarkdownLinkHref',
'chatMarkdownLinkTarget'
]);


Expand All @@ -56,6 +58,7 @@ export const OramaChatBox = /*@__PURE__*/ defineContainer<JSX.OramaChatBox>('ora
'systemPrompts',
'chatMarkdownLinkTitle',
'chatMarkdownLinkHref',
'chatMarkdownLinkTarget',
'answerGenerated',
'answerSourceClick',
'chatMarkdownLinkClicked'
Expand All @@ -74,6 +77,7 @@ export const OramaChatMessagesContainer = /*@__PURE__*/ defineContainer<JSX.Oram
'interactions',
'chatMarkdownLinkTitle',
'chatMarkdownLinkHref',
'chatMarkdownLinkTarget',
'answerGenerated'
]);

Expand Down Expand Up @@ -124,6 +128,7 @@ export const OramaMarkdown = /*@__PURE__*/ defineContainer<JSX.OramaMarkdown>('o
'content',
'chatMarkdownLinkTitle',
'chatMarkdownLinkHref',
'chatMarkdownLinkTarget',
'chatMarkdownLinkClicked'
]);

Expand Down Expand Up @@ -182,6 +187,7 @@ export const OramaSearchBox = /*@__PURE__*/ defineContainer<JSX.OramaSearchBox>(
'searchParams',
'chatMarkdownLinkTitle',
'chatMarkdownLinkHref',
'chatMarkdownLinkTarget',
'searchCompleted',
'searchResultClick',
'answerGenerated',
Expand Down
16 changes: 14 additions & 2 deletions packages/ui-stencil/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import { HTMLStencilElement, JSXBase } from "@stencil/core/internal";
import { ButtonProps } from "./components/internal/orama-button/orama-button";
import { ChatMarkdownLinkHref, ChatMarkdownLinkTitle, CloudIndexConfig, ColorScheme, Facet, OnAnswerGeneratedCallbackProps, OnAnswerSourceClickCallbackProps, OnChatMarkdownLinkClickedCallbackProps, OnSearchCompletedCallbackProps, OnSearchResultClickCallbackProps, ResultMap, SearchResult, SearchResultBySection, SourcesMap } from "./types/index";
import { ChatMarkdownLinkHref, ChatMarkdownLinkTarget, ChatMarkdownLinkTitle, CloudIndexConfig, ColorScheme, Facet, OnAnswerGeneratedCallbackProps, OnAnswerSourceClickCallbackProps, OnChatMarkdownLinkClickedCallbackProps, OnSearchCompletedCallbackProps, OnSearchResultClickCallbackProps, ResultMap, SearchResult, SearchResultBySection, SourcesMap } from "./types/index";
import { TChatInteraction } from "./context/chatContext";
import { OramaClient } from "@oramacloud/client";
import { InputProps } from "./components/internal/orama-input/orama-input";
Expand All @@ -18,7 +18,7 @@ import { TThemeOverrides as TThemeOverrides1 } from "./components.d";
import { SearchResultsProps } from "./components/internal/orama-search-results/orama-search-results";
import { TextProps } from "./components/internal/orama-text/orama-text";
export { ButtonProps } from "./components/internal/orama-button/orama-button";
export { ChatMarkdownLinkHref, ChatMarkdownLinkTitle, CloudIndexConfig, ColorScheme, Facet, OnAnswerGeneratedCallbackProps, OnAnswerSourceClickCallbackProps, OnChatMarkdownLinkClickedCallbackProps, OnSearchCompletedCallbackProps, OnSearchResultClickCallbackProps, ResultMap, SearchResult, SearchResultBySection, SourcesMap } from "./types/index";
export { ChatMarkdownLinkHref, ChatMarkdownLinkTarget, ChatMarkdownLinkTitle, CloudIndexConfig, ColorScheme, Facet, OnAnswerGeneratedCallbackProps, OnAnswerSourceClickCallbackProps, OnChatMarkdownLinkClickedCallbackProps, OnSearchCompletedCallbackProps, OnSearchResultClickCallbackProps, ResultMap, SearchResult, SearchResultBySection, SourcesMap } from "./types/index";
export { TChatInteraction } from "./context/chatContext";
export { OramaClient } from "@oramacloud/client";
export { InputProps } from "./components/internal/orama-input/orama-input";
Expand All @@ -41,6 +41,7 @@ export namespace Components {
}
interface OramaChat {
"chatMarkdownLinkHref"?: ChatMarkdownLinkHref;
"chatMarkdownLinkTarget"?: ChatMarkdownLinkTarget;
"chatMarkdownLinkTitle"?: ChatMarkdownLinkTitle;
"defaultTerm"?: string;
"focusInput"?: boolean;
Expand All @@ -55,12 +56,14 @@ export namespace Components {
}
interface OramaChatAssistentMessage {
"chatMarkdownLinkHref"?: ChatMarkdownLinkHref;
"chatMarkdownLinkTarget"?: ChatMarkdownLinkTarget;
"chatMarkdownLinkTitle"?: ChatMarkdownLinkTitle;
"interaction": TChatInteraction;
}
interface OramaChatBox {
"autoFocus": boolean;
"chatMarkdownLinkHref"?: ChatMarkdownLinkHref;
"chatMarkdownLinkTarget"?: ChatMarkdownLinkTarget;
"chatMarkdownLinkTitle"?: ChatMarkdownLinkTitle;
"clientInstance"?: OramaClient;
"index"?: CloudIndexConfig;
Expand All @@ -80,6 +83,7 @@ export namespace Components {
}
interface OramaChatMessagesContainer {
"chatMarkdownLinkHref"?: ChatMarkdownLinkHref;
"chatMarkdownLinkTarget"?: ChatMarkdownLinkTarget;
"chatMarkdownLinkTitle"?: ChatMarkdownLinkTitle;
"interactions": TChatInteraction[];
}
Expand Down Expand Up @@ -114,6 +118,7 @@ export namespace Components {
}
interface OramaMarkdown {
"chatMarkdownLinkHref"?: ChatMarkdownLinkHref;
"chatMarkdownLinkTarget"?: ChatMarkdownLinkTarget;
"chatMarkdownLinkTitle"?: ChatMarkdownLinkTitle;
"content": string;
}
Expand Down Expand Up @@ -141,6 +146,7 @@ export namespace Components {
}
interface OramaSearchBox {
"chatMarkdownLinkHref"?: ChatMarkdownLinkHref;
"chatMarkdownLinkTarget"?: ChatMarkdownLinkTarget;
"chatMarkdownLinkTitle"?: ChatMarkdownLinkTitle;
"chatPlaceholder"?: string;
"clientInstance"?: OramaClient;
Expand Down Expand Up @@ -592,6 +598,7 @@ declare namespace LocalJSX {
}
interface OramaChat {
"chatMarkdownLinkHref"?: ChatMarkdownLinkHref;
"chatMarkdownLinkTarget"?: ChatMarkdownLinkTarget;
"chatMarkdownLinkTitle"?: ChatMarkdownLinkTitle;
"defaultTerm"?: string;
"focusInput"?: boolean;
Expand All @@ -607,12 +614,14 @@ declare namespace LocalJSX {
}
interface OramaChatAssistentMessage {
"chatMarkdownLinkHref"?: ChatMarkdownLinkHref;
"chatMarkdownLinkTarget"?: ChatMarkdownLinkTarget;
"chatMarkdownLinkTitle"?: ChatMarkdownLinkTitle;
"interaction"?: TChatInteraction;
}
interface OramaChatBox {
"autoFocus"?: boolean;
"chatMarkdownLinkHref"?: ChatMarkdownLinkHref;
"chatMarkdownLinkTarget"?: ChatMarkdownLinkTarget;
"chatMarkdownLinkTitle"?: ChatMarkdownLinkTitle;
"clientInstance"?: OramaClient;
"index"?: CloudIndexConfig;
Expand Down Expand Up @@ -644,6 +653,7 @@ declare namespace LocalJSX {
}
interface OramaChatMessagesContainer {
"chatMarkdownLinkHref"?: ChatMarkdownLinkHref;
"chatMarkdownLinkTarget"?: ChatMarkdownLinkTarget;
"chatMarkdownLinkTitle"?: ChatMarkdownLinkTitle;
"interactions"?: TChatInteraction[];
"onAnswerGenerated"?: (event: OramaChatMessagesContainerCustomEvent<OnSearchCompletedCallbackProps>) => void;
Expand Down Expand Up @@ -680,6 +690,7 @@ declare namespace LocalJSX {
}
interface OramaMarkdown {
"chatMarkdownLinkHref"?: ChatMarkdownLinkHref;
"chatMarkdownLinkTarget"?: ChatMarkdownLinkTarget;
"chatMarkdownLinkTitle"?: ChatMarkdownLinkTitle;
"content"?: string;
"onChatMarkdownLinkClicked"?: (event: OramaMarkdownCustomEvent<OnChatMarkdownLinkClickedCallbackProps>) => void;
Expand Down Expand Up @@ -711,6 +722,7 @@ declare namespace LocalJSX {
}
interface OramaSearchBox {
"chatMarkdownLinkHref"?: ChatMarkdownLinkHref;
"chatMarkdownLinkTarget"?: ChatMarkdownLinkTarget;
"chatMarkdownLinkTitle"?: ChatMarkdownLinkTitle;
"chatPlaceholder"?: string;
"clientInstance"?: OramaClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import '@phosphor-icons/webcomponents/dist/icons/PhThumbsDown.mjs'
import '@phosphor-icons/webcomponents/dist/icons/PhWarning.mjs'
import { chatContext, TAnswerStatus } from '@/context/chatContext'
import { copyToClipboard } from '@/utils/utils'
import type { ChatMarkdownLinkHref, ChatMarkdownLinkTitle } from '@/types'
import type { ChatMarkdownLinkHref, ChatMarkdownLinkTarget, ChatMarkdownLinkTitle } from '@/types'

@Component({
tag: 'orama-chat-assistent-message',
Expand All @@ -17,6 +17,7 @@ export class OramaChatAssistentMessage {
@Prop() interaction: TChatInteraction
@Prop() chatMarkdownLinkTitle?: ChatMarkdownLinkTitle
@Prop() chatMarkdownLinkHref?: ChatMarkdownLinkHref
@Prop() chatMarkdownLinkTarget?: ChatMarkdownLinkTarget

@State() isCopied = false
handleCopyToClipboard = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { Component, Element, type EventEmitter, Prop, Watch, h, Event } from '@s
import DOMPurify from 'dompurify'
import hljs from 'highlight.js/lib/core'
import { marked } from 'marked'
import type { ChatMarkdownLinkHref, ChatMarkdownLinkTitle, OnChatMarkdownLinkClickedCallbackProps } from '@/types'
import type {
ChatMarkdownLinkHref,
ChatMarkdownLinkTarget,
ChatMarkdownLinkTitle,
OnChatMarkdownLinkClickedCallbackProps,
} from '@/types'

// biome-ignore lint/suspicious/noExplicitAny: Let me be, TypeScript
;(window as any).hljs = hljs
Expand Down Expand Up @@ -90,6 +95,7 @@ export class OramaMarkdown {
@Prop() content: string
@Prop() chatMarkdownLinkTitle?: ChatMarkdownLinkTitle
@Prop() chatMarkdownLinkHref?: ChatMarkdownLinkHref
@Prop() chatMarkdownLinkTarget?: ChatMarkdownLinkTarget

@Event({ bubbles: true, composed: true, cancelable: true })
chatMarkdownLinkClicked: EventEmitter<OnChatMarkdownLinkClickedCallbackProps>
Expand All @@ -110,6 +116,11 @@ export class OramaMarkdown {
const link = document.createElement('a')
link.innerHTML = this.chatMarkdownLinkTitle?.({ href: token.href, text: token.text }) ?? token.text
link.href = this.chatMarkdownLinkHref?.({ href: token.href, text: token.text }) ?? token.href

if (this.chatMarkdownLinkHref) {
link.target = this.chatMarkdownLinkHref?.({ href: token.href, text: token.text })
}

link.onclick = (onClickEvent) => {
const chatMarkdownLinkClicked = this.chatMarkdownLinkClicked.emit({
text: token.text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

## Properties

| Property | Attribute | Description | Type | Default |
| ----------------------- | --------- | ----------- | ------------------------------------------------------------- | ----------- |
| `chatMarkdownLinkHref` | -- | | `({ text, href }: { text: string; href: string; }) => string` | `undefined` |
| `chatMarkdownLinkTitle` | -- | | `({ text, href }: { text: string; href: string; }) => string` | `undefined` |
| `content` | `content` | | `string` | `undefined` |
| Property | Attribute | Description | Type | Default |
| ------------------------ | --------- | ----------- | ------------------------------------------------------------- | ----------- |
| `chatMarkdownLinkHref` | -- | | `({ text, href }: { text: string; href: string; }) => string` | `undefined` |
| `chatMarkdownLinkTarget` | -- | | `({ text, href }: { text: string; href: string; }) => string` | `undefined` |
| `chatMarkdownLinkTitle` | -- | | `({ text, href }: { text: string; href: string; }) => string` | `undefined` |
| `content` | `content` | | `string` | `undefined` |


## Events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

## Properties

| Property | Attribute | Description | Type | Default |
| ----------------------- | --------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- |
| `chatMarkdownLinkHref` | -- | | `({ text, href }: { text: string; href: string; }) => string` | `undefined` |
| `chatMarkdownLinkTitle` | -- | | `({ text, href }: { text: string; href: string; }) => string` | `undefined` |
| `interaction` | -- | | `{ query: string; response?: string; sources?: any; latest?: boolean; status: TAnswerStatus; interactionId?: string; relatedQueries?: string[]; }` | `undefined` |
| Property | Attribute | Description | Type | Default |
| ------------------------ | --------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- |
| `chatMarkdownLinkHref` | -- | | `({ text, href }: { text: string; href: string; }) => string` | `undefined` |
| `chatMarkdownLinkTarget` | -- | | `({ text, href }: { text: string; href: string; }) => string` | `undefined` |
| `chatMarkdownLinkTitle` | -- | | `({ text, href }: { text: string; href: string; }) => string` | `undefined` |
| `interaction` | -- | | `{ query: string; response?: string; sources?: any; latest?: boolean; status: TAnswerStatus; interactionId?: string; relatedQueries?: string[]; }` | `undefined` |


## Dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Component, Host, Prop, h, Element, State, type EventEmitter, Event } from '@stencil/core'
import { chatContext, type TChatInteraction } from '@/context/chatContext'
import type { ChatMarkdownLinkHref, ChatMarkdownLinkTitle, OnSearchCompletedCallbackProps } from '@/types'
import type {
ChatMarkdownLinkHref,
ChatMarkdownLinkTarget,
ChatMarkdownLinkTitle,
OnSearchCompletedCallbackProps,
} from '@/types'

@Component({
tag: 'orama-chat-messages-container',
Expand All @@ -11,6 +16,7 @@ export class OramaChatMessagesContainer {
@Prop() interactions: TChatInteraction[]
@Prop() chatMarkdownLinkTitle?: ChatMarkdownLinkTitle
@Prop() chatMarkdownLinkHref?: ChatMarkdownLinkHref
@Prop() chatMarkdownLinkTarget?: ChatMarkdownLinkTarget

@Event({ bubbles: true, composed: true }) answerGenerated: EventEmitter<OnSearchCompletedCallbackProps>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

## Properties

| Property | Attribute | Description | Type | Default |
| ----------------------- | --------- | ----------- | ------------------------------------------------------------- | ----------- |
| `chatMarkdownLinkHref` | -- | | `({ text, href }: { text: string; href: string; }) => string` | `undefined` |
| `chatMarkdownLinkTitle` | -- | | `({ text, href }: { text: string; href: string; }) => string` | `undefined` |
| `interactions` | -- | | `TChatInteraction[]` | `undefined` |
| Property | Attribute | Description | Type | Default |
| ------------------------ | --------- | ----------- | ------------------------------------------------------------- | ----------- |
| `chatMarkdownLinkHref` | -- | | `({ text, href }: { text: string; href: string; }) => string` | `undefined` |
| `chatMarkdownLinkTarget` | -- | | `({ text, href }: { text: string; href: string; }) => string` | `undefined` |
| `chatMarkdownLinkTitle` | -- | | `({ text, href }: { text: string; href: string; }) => string` | `undefined` |
| `interactions` | -- | | `TChatInteraction[]` | `undefined` |


## Events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Component, Fragment, Listen, Host, Prop, State, Watch, h, type EventEmi
import { chatContext, chatStore, TAnswerStatus } from '@/context/chatContext'
import type {
ChatMarkdownLinkHref,
ChatMarkdownLinkTarget,
ChatMarkdownLinkTitle,
OnAnswerGeneratedCallbackProps,
SearchResult,
Expand Down Expand Up @@ -31,6 +32,7 @@ export class OramaChat {

@Prop() chatMarkdownLinkTitle?: ChatMarkdownLinkTitle
@Prop() chatMarkdownLinkHref?: ChatMarkdownLinkHref
@Prop() chatMarkdownLinkTarget?: ChatMarkdownLinkTarget

@Event({ bubbles: true, composed: true }) answerGenerated: EventEmitter<OnAnswerGeneratedCallbackProps>

Expand Down
Loading

0 comments on commit d78268a

Please sign in to comment.