Markprompt Web
A prebuilt version of the Markprompt dialog, based on @markprompt/react
, built
with Preact for bundle-size savings. Viable for use from vanilla JavaScript or
any framework.
Installation
Install the package from NPM:
npm add @markprompt/web @markprompt/css
Usage
Include the CSS on your page, via a link tag or by importing it in your
JavaScript:
<link rel="stylesheet" href="https://esm.sh/@markprompt/css@0.22.0?css" />
import '@markprompt/css';
Call the markprompt
function with your project key:
import { markprompt } from '@markprompt/web';
const markpromptEl = document.querySelector('#markprompt');
markprompt('YOUR-PROJECT-KEY', markpromptEl, {
references: {
getHref: (reference) => reference.file.path.replace(/\.[^.]+$/, '');
getLabel: (reference) => {
return reference.meta?.leadHeading?.value || reference.file?.title;
}
},
});
where YOUR-PROJECT-KEY
can be obtained in your project settings on
Markprompt.com.
Options are optional and allow you to configure the texts and links used in the
component to some extent. You will most likely want to pass references.getHref
and reference.getLabel
to transform your prompt references into links to your
corresponding documentation, and search.getHref
to transform search result
paths into links to your documentation.
import type {
SubmitFeedbackOptions,
SubmitPromptOptions,
SubmitSearchQueryOptions,
} from '@markprompt/core';
interface MarkpromptOptions {
children?: React.ReactNode;
display?: 'plain' | 'dialog';
sticky?: boolean;
defaultView?: View;
layout?: 'panels' | 'tabs';
close?: {
label?: string;
visible?: boolean;
hasIcon?: boolean;
};
description?: {
hide?: boolean;
text?: string;
};
feedback?: SubmitFeedbackOptions & {
enabled?: boolean;
heading?: string;
onFeedbackSubmit?: (
feedback: PromptFeedback,
messages: ChatViewMessage[],
messageId?: string,
) => void;
};
chat?: UserConfigurableOptions & {
enabled?: boolean;
label?: string;
tabLabel?: string;
placeholder?: string;
buttonLabel?: string;
errorText?: ComponentType<{ error: Error }>;
showCopy?: boolean;
history?: boolean;
defaultView?: DefaultViewProps;
avatars?: {
visible?: boolean;
user?: string | ComponentType<{ className: string }>;
assistant?: string | ComponentType<{ className: string }>;
};
};
references?: {
display?: 'none' | 'end';
getHref?: (reference: FileSectionReference) => string | undefined;
getLabel?: (reference: FileSectionReference) => string | undefined;
heading?: string;
loadingText?: string;
transformReferenceId?: (referenceId: string) => {
href: string;
text: string;
};
};
search?: SubmitSearchQueryOptions & {
enabled?: boolean;
getHref?: (
reference: SearchResult | AlgoliaDocSearchHit,
) => string | undefined;
getHeading?: (
reference: SearchResult | AlgoliaDocSearchHit,
query: string,
) => string | undefined;
getTitle?: (
reference: SearchResult | AlgoliaDocSearchHit,
query: string,
) => string | undefined;
getSubtitle?: (
reference: SearchResult | AlgoliaDocSearchHit,
query: string,
) => string | undefined;
label?: string;
askLabel?: string;
defaultView?: DefaultSearchViewProps;
tabLabel?: string;
placeholder?: string;
};
trigger?: {
label?: string;
buttonLabel?: string;
placeholder?: string;
floating?: boolean;
customElement?: boolean | ReactNode;
iconSrc?: string;
};
title?: {
hide?: boolean;
text?: string;
};
linkAs?: string | ComponentType<any>;
showBranding?: boolean;
branding?: {
show?: boolean;
type?: 'plain' | 'text';
};
debug?: boolean;
}
Styles are easily overridable for customization via targeting classes.
Additionally, see the styling section in
our documentation for a full list of variables.
Usage via <script>
tag
Besides initializing the Markprompt component yourselves from JavaScript, you
can load the script from a CDN. You can attach the options for the Markprompt
component to the window prior to loading our script:
<link rel="stylesheet" href="https://esm.sh/@markprompt/css@0.22.0?css" />
<script>
window.markprompt = {
projectKey: `YOUR-PROJECT-KEY`,
container: `#markprompt`,
options: {
references: {
getHref: (reference) => reference.file?.path?.replace(/\.[^.]+$/, ''),
getLabel: (reference) => {
return reference.meta?.leadHeading?.value || reference.file?.title;
},
},
},
};
</script>
<script type="module" src="https://esm.sh/@markprompt/web@0.26.0/init"></script>
<div id="markprompt"></div>
API
markprompt(projectKey, container, options?)
Render a Markprompt dialog button.
Arguments
projectKey
(string
): Your Markprompt project key.container
(HTMLElement | string
): The element or selector to render
Markprompt into.options
(object
): Options for customizing Markprompt, see above.
When rendering the Markprompt component, it will render a search input-like
button by default. You have two other options:
- set
trigger.floating = true
to render a floating button - set
trigger.customElement = true
, then
import { openMarkprompt } from '@markprompt/react'
and call
openMarkprompt()
from your code. This gives you the flexibility to render
your own trigger element and attach whatever event handlers you would like
and/or open the Markprompt dialog programmatically.
markpromptOpen()
Open the Markprompt dialog programmatically.
markpromptClose()
Close the Markprompt dialog programmatically.
markpromptChat(projectKey, container, options?)
Render the Markprompt chat view standalone, outside of a dialog.
projectKey
(string
): Your Markprompt project key.container
(HTMLElement | string
): The element or selector to render
Markprompt into.options
(object
): Options for customizing Markprompt, see above.options.chatOptions
(MarkpromptOptions.chat
): Enable and configure chat
functionality. Allows users to have a conversation with an assistant.
Enabling chat functionality will disable prompt functionality. See above for
options.options.debug
(boolean
): Display debug infooptions.feedbackOptions
(MarkpromptOptions.feedback
): Enable feedback
functionality, shows a thumbs up/down button after a prompt was submitted.
See above for options.options.referencesOptions
(MarkpromptOptions.references
): Enable and
configure references functionality. See above for options.
Documentation
The full documentation for the package can be found on the
Markprompt docs.
Authors
This library is created by the team behind Markprompt
(@markprompt).
License
MIT © Markprompt