Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(notebooks): link to insight from markdown notebook blocks (#70094)

Marius Andra2c9b9a40a28d
diff --git a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
index 59d97597434d..e20d29730688 100644
--- a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
+++ b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
@@ -275,6 +275,9 @@ export type MarkdownNotebookProps = {
/** In view mode, keep the filters toggle for definitions with `viewModeFilters` — for
* read-only canvases where the filters panel is the only way to configure a node. */
allowViewModeFilters?: boolean
+ /** Public/shared read-only renders: hide per-block resource links whose relative URLs would
+ * resolve against the viewer's project rather than the author's. */
+ hideResourceLinks?: boolean
placeholder?: string
className?: string
autoFocus?: boolean
@@ -584,6 +587,7 @@ function MarkdownNotebookEditor({
focusAIPromptRequest,
aiWritingNodeIndexes,
allowViewModeFilters = false,
+ hideResourceLinks = false,
placeholder = 'Start writing...',
className,
autoFocus = false,
@@ -5846,6 +5850,7 @@ function MarkdownNotebookEditor({
rememberedComponentPanels: componentPanelCacheEntry?.remembered,
persistComponentPanelVisibility,
allowViewModeFilters,
+ hideResourceLinks,
isSelected: selectedComponentNodeIds.has(node.id),
toggleComponentPanel: (panel) => {
const nextPanels = {
diff --git a/frontend/src/lib/components/MarkdownNotebook/NotebookComponentShell.tsx b/frontend/src/lib/components/MarkdownNotebook/NotebookComponentShell.tsx
index 3f440c1dc72c..8d5654aa4ae9 100644
--- a/frontend/src/lib/components/MarkdownNotebook/NotebookComponentShell.tsx
+++ b/frontend/src/lib/components/MarkdownNotebook/NotebookComponentShell.tsx
@@ -16,6 +16,7 @@ import {
IconDatabase,
IconEllipsis,
IconExpand,
+ IconExternal,
IconEye,
IconPencil,
IconGraph,
@@ -66,6 +67,10 @@ export type NotebookComponentShellProps = {
persistComponentPanelVisibility: boolean
/** Surface-level opt-in for definitions with `viewModeFilters` (read-only canvases). */
allowViewModeFilters?: boolean
+ /** Public/shared read-only renders: hide the resource link, whose relative URL resolves
+ * against the viewer's project (wrong project or 404 for logged-in viewers, unusable for
+ * anonymous ones). Mirrors the legacy notebook's `!isShared` gate. */
+ hideResourceLinks?: boolean
isSelected: boolean
registry: NotebookComponentRegistry
toggleComponentPanel: (panel: ComponentPanel) => void
@@ -86,6 +91,7 @@ export function NotebookComponentShell({
rememberedComponentPanels,
persistComponentPanelVisibility,
allowViewModeFilters,
+ hideResourceLinks,
isSelected,
registry,
toggleComponentPanel,
@@ -120,6 +126,10 @@ export function NotebookComponentShell({
const hasOpenComponentPanel = componentPanels.filters || componentPanels.results
const titleDisplay = getComponentTitleDisplay(node, definition)
const toolbarTitle = getComponentToolbarTitle(node, definition, titleDisplay.label)
+ const href = definition?.getHref?.(node) ?? null
+ // Suppress the link on public/shared renders (see hideResourceLinks): its relative URL would
+ // resolve against the viewer's own project rather than the notebook author's.
+ const showResourceLink = !!href && !hideResourceLinks
// The user-set title (props.title) wins; the computed contextual title is the watermark/fallback.
// A title equal to the component's own label (e.g. code blocks default to "Python") is treated
// as "no user title" so the field reads as empty by default.
@@ -416,7 +426,7 @@ export function NotebookComponentShell({
{resolvedTitle}
</div>
) : null}
- {mode === 'edit' || toolbarMenuItems || showCollapseToggle ? (
+ {showResourceLink || mode === 'edit' || toolbarMenuItems || showCollapseToggle ? (
<div className="MarkdownNotebook__component-actions">
{showCollapseToggle ? (
<LemonButton
@@ -437,6 +447,16 @@ export function NotebookComponentShell({
/>
</LemonMenu>
) : null}
+ {showResourceLink ? (
+ <LemonButton
+ aria-label="Open in new tab"
+ size="xsmall"
+ icon={<IconExternal />}
+ tooltip="Open in new tab"
+ to={href ?? undefined}
+ targetBlank
+ />
+ ) : null}
{mode === 'edit' ? (
<LemonButton
aria-label="Delete component"
diff --git a/frontend/src/lib/components/MarkdownNotebook/renderNode.tsx b/frontend/src/lib/components/MarkdownNotebook/renderNode.tsx
index fd559330cc6a..35fafafd7b96 100644
--- a/frontend/src/lib/components/MarkdownNotebook/renderNode.tsx
+++ b/frontend/src/lib/components/MarkdownNotebook/renderNode.tsx
@@ -35,6 +35,7 @@ export function renderNode({
rememberedComponentPanels,
persistComponentPanelVisibility,
allowViewModeFilters,
+ hideResourceLinks,
isSelected,
toggleComponentPanel,
setLocalComponentPanels,
@@ -83,6 +84,7 @@ export function renderNode({
rememberedComponentPanels?: ComponentPanelVisibility
persistComponentPanelVisibility: boolean
allowViewModeFilters?: boolean
+ hideResourceLinks?: boolean
isSelected: boolean
toggleComponentPanel: (panel: ComponentPanel) => void
setLocalComponentPanels: (nodeId: string, panels: ComponentPanelVisibility) => void
@@ -185,6 +187,7 @@ export function renderNode({
rememberedComponentPanels={rememberedComponentPanels}
persistComponentPanelVisibility={persistComponentPanelVisibility}
allowViewModeFilters={allowViewModeFilters}
+ hideResourceLinks={hideResourceLinks}
setLocalComponentPanels={setLocalComponentPanels}
rememberComponentPanels={rememberComponentPanels}
setBlockRef={setBlockRef}
diff --git a/frontend/src/lib/components/MarkdownNotebook/types.ts b/frontend/src/lib/components/MarkdownNotebook/types.ts
index 14df5bf3f0d0..3fbf7e9968ad 100644
--- a/frontend/src/lib/components/MarkdownNotebook/types.ts
+++ b/frontend/src/lib/components/MarkdownNotebook/types.ts
@@ -163,6 +163,8 @@ export type NotebookComponentDefinition = {
defaultProps?: NotebookComponentProps | (() => NotebookComponentProps)
validateProps?: (props: NotebookComponentProps) => string[]
getTitle?: (node: NotebookComponentBlockNode) => string | null | undefined
+ /** Canonical PostHog URL the block points at (e.g. the insight, recording, or person it renders), opened in a new tab from the toolbar. */
+ getHref?: (node: NotebookComponentBlockNode) => string | null | undefined
ViewComponent: (props: NotebookComponentRenderProps) => JSX.Element
EditComponent?: (props: NotebookComponentRenderProps) => JSX.Element
exclusiveEditPanel?: boolean
diff --git a/frontend/src/scenes/notebooks/Notebook/MarkdownNotebookV2Renderer.tsx b/frontend/src/scenes/notebooks/Notebook/MarkdownNotebookV2Renderer.tsx
index 30f866e70388..a826f6b6c70e 100644
--- a/frontend/src/scenes/notebooks/Notebook/MarkdownNotebookV2Renderer.tsx
+++ b/frontend/src/scenes/notebooks/Notebook/MarkdownNotebookV2Renderer.tsx
@@ -89,8 +89,14 @@ type MarkdownNotebookEntityPickerKind =
export function MarkdownNotebookV2({ debugOpen, onDebugOpenChange }: MarkdownNotebookV2Props): JSX.Element {
const mountedNotebookLogic = useMountedLogic(notebookLogic)
- const { isEditable, notebook, markdownEditorValue, markdownEditorInteractionActive, markdownRemoteCarets } =
- useValues(notebookLogic)
+ const {
+ isEditable,
+ isShared,
+ notebook,
+ markdownEditorValue,
+ markdownEditorInteractionActive,
+ markdownRemoteCarets,
+ } = useValues(notebookLogic)
const { featureFlags } = useValues(featureFlagLogic)
const markdownRegistry = useMemo(() => getMarkdownRegistryForFeatureFlags(featureFlags), [featureFlags])
const hiddenInsertCommandKeys = useMemo(
@@ -749,6 +755,7 @@ export function MarkdownNotebookV2({ debugOpen, onDebugOpenChange }: MarkdownNot
remoteValue={remoteMarkdown}
remoteVersion={notebook?.version}
mode={isEditable ? 'edit' : 'view'}
+ hideResourceLinks={isShared}
registry={markdownRegistry}
extraInsertCommands={isEditable ? buildExtraInsertCommands : undefined}
hiddenInsertCommandKeys={hiddenInsertCommandKeys}
diff --git a/frontend/src/scenes/notebooks/Notebook/markdownNotebookRegistry.tsx b/frontend/src/scenes/notebooks/Notebook/markdownNotebookRegistry.tsx
index fde864542683..8f9b2c939032 100644
--- a/frontend/src/scenes/notebooks/Notebook/markdownNotebookRegistry.tsx
+++ b/frontend/src/scenes/notebooks/Notebook/markdownNotebookRegistry.tsx
@@ -255,6 +255,7 @@ export const NOTEBOOK_MARKDOWN_REGISTRY: NotebookComponentRegistry = createMarkd
insertCommand: definition.insertCommand,
getTitle: (node: NotebookComponentBlockNode) =>
getMarkdownNotebookNodeTitle(node, nodeType, options, label),
+ getHref: (node: NotebookComponentBlockNode) => getMarkdownNotebookNodeHref(node, nodeType, options),
}
}),
{
@@ -361,6 +362,21 @@ export function getMarkdownNotebookNodeTitle(
)
}
+export function getMarkdownNotebookNodeHref(
+ node: NotebookComponentBlockNode,
+ nodeType: NotebookNodeType | undefined,
+ options: CreatePostHogWidgetNodeOptions<any> | null
+): string | null {
+ // Reuse the legacy node's `href` (e.g. Query → urls.insightView/urls.insightNew) so saved and
+ // ad-hoc insights, recordings, persons, etc. all expose a link to the underlying resource.
+ if (!options?.href) {
+ return null
+ }
+ const attributes = getNodeAttributes(node.props, node.id, options, nodeType, false)
+ const href = typeof options.href === 'function' ? options.href(attributes) : options.href
+ return href ?? null
+}
+
export function getNotebookStringProp(value: NotebookPropValue | undefined): string | null {
return typeof value === 'string' ? value : null
}