@openpeepshq/react 0.5.1 → 0.6.0

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.
@@ -1,5 +1,7 @@
1
1
  export interface CompiledMarkdownProps {
2
2
  html: string;
3
3
  className?: string;
4
+ /** When false, HTML is rendered as-is without sanitization (for trusted sources). */
5
+ sanitize?: boolean;
4
6
  }
5
- export declare const CompiledMarkdown: ({ html, className, }: CompiledMarkdownProps) => import("react/jsx-runtime").JSX.Element;
7
+ export declare const CompiledMarkdown: ({ html, className, sanitize, }: CompiledMarkdownProps) => import("react/jsx-runtime").JSX.Element;
@@ -1,4 +1,7 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useMemo } from 'react';
3
+ import DOMPurify from 'dompurify';
4
+ import { injectIframeSafetyAttrs, MARKDOWN_HTML_ALLOWED_TAGS, MARKDOWN_HTML_ALLOWED_ATTRS, MARKDOWN_HTML_FORBIDDEN_TAGS, MARKDOWN_HTML_FORBIDDEN_ATTRS, } from '@openpeepshq/common/lib';
2
5
  import { OPENPEEPS_MARKDOWN_PROSE_CLASS } from './classes.js';
3
6
  import { isExternalLink } from './linkTargets.js';
4
7
  import { recordOutboundClick } from '../../lib/analyticsClicks.js';
@@ -14,4 +17,17 @@ const onMarkdownClick = (event) => {
14
17
  recordOutboundClick(href, origin);
15
18
  }
16
19
  };
17
- export const CompiledMarkdown = ({ html, className, }) => (_jsx("div", { className: className ?? OPENPEEPS_MARKDOWN_PROSE_CLASS, dangerouslySetInnerHTML: { __html: html }, onClick: onMarkdownClick }));
20
+ const sanitizeHtml = (html) => {
21
+ let clean = DOMPurify.sanitize(html, {
22
+ ALLOWED_TAGS: [...MARKDOWN_HTML_ALLOWED_TAGS],
23
+ ALLOWED_ATTR: [...MARKDOWN_HTML_ALLOWED_ATTRS],
24
+ FORBID_TAGS: [...MARKDOWN_HTML_FORBIDDEN_TAGS],
25
+ FORBID_ATTR: [...MARKDOWN_HTML_FORBIDDEN_ATTRS],
26
+ });
27
+ clean = injectIframeSafetyAttrs(clean);
28
+ return clean;
29
+ };
30
+ export const CompiledMarkdown = ({ html, className, sanitize = true, }) => {
31
+ const safeHtml = useMemo(() => (sanitize ? sanitizeHtml(html) : html), [html, sanitize]);
32
+ return (_jsx("div", { className: className ?? OPENPEEPS_MARKDOWN_PROSE_CLASS, dangerouslySetInnerHTML: { __html: safeHtml }, onClick: onMarkdownClick }));
33
+ };
@@ -1,16 +1,12 @@
1
+ import type { MarkdownRenderProps } from '@openpeepshq/common/lib';
2
+ /** @deprecated Use {@link MentionProfileRef} from @openpeepshq/common. */
1
3
  export type MarkdownMention = {
2
- text?: string;
3
4
  profile: {
4
5
  handle: string;
5
6
  displayName?: string | null;
6
7
  };
7
8
  };
8
- export interface OpenpeepsMarkdownProps {
9
- source?: string;
10
- mentions?: MarkdownMention[];
9
+ export interface OpenpeepsMarkdownProps extends MarkdownRenderProps {
11
10
  className?: string;
12
- linkPreviewMode?: 'prepend' | 'append' | 'none';
13
- /** When true, all links open in a new tab. External links always do. */
14
- newTab?: boolean;
15
11
  }
16
12
  export declare function OpenpeepsMarkdown({ source, mentions, className, linkPreviewMode, newTab, }: OpenpeepsMarkdownProps): import("react/jsx-runtime").JSX.Element;
@@ -1,17 +1,17 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { useMemo } from 'react';
3
- import { linkProfileMentions } from '@openpeepshq/common/lib';
3
+ import { preprocessMarkdownSource } from '@openpeepshq/common/lib';
4
4
  import { extractUrlsFromText, isEmail } from '../preview-link/helpers.js';
5
5
  import { PreviewLink } from '../preview-link/PreviewLink.js';
6
6
  import { OPENPEEPS_MARKDOWN_PROSE_CLASS, OPENPEEPS_MARKDOWN_STATIC_CLASS, } from './classes.js';
7
7
  import { compileMarkdownToHtml } from './compileMarkdown.js';
8
8
  import { resolveStaticUrl, useStaticRender } from './staticRender.js';
9
- const hashtagPattern = /(^|[\s(>])#([a-z0-9_]+)(?=\s|$|[.,!?;:])/gi;
10
- function preprocessSource(source, mentions = []) {
11
- let text = linkProfileMentions(source, mentions);
9
+ const hashtagPattern = /(^|[\s(>])#([a-z0-9_]+)(?=\s|$|[.,!?;:])$/gi;
10
+ const preprocessWithHashtags = (source, mentions = []) => {
11
+ let text = preprocessMarkdownSource(source, mentions);
12
12
  text = text.replace(hashtagPattern, (_, prefix, tag) => `${prefix}[#${tag}](/tags/${encodeURIComponent(tag)})`);
13
13
  return text;
14
- }
14
+ };
15
15
  const markdownOrigin = (staticRender, baseUrl) => {
16
16
  if (staticRender && baseUrl) {
17
17
  try {
@@ -39,8 +39,12 @@ export function OpenpeepsMarkdown({ source, mentions = [], className, linkPrevie
39
39
  .filter((url) => !isEmail(url));
40
40
  }, [source]);
41
41
  const html = useMemo(() => {
42
- const processed = preprocessSource(source ?? '', mentions);
43
- let out = compileMarkdownToHtml(processed, { origin, newTab });
42
+ const processed = preprocessWithHashtags(source ?? '', mentions);
43
+ let out = compileMarkdownToHtml(processed, {
44
+ origin,
45
+ newTab,
46
+ sanitize: true,
47
+ });
44
48
  if (staticRender && baseUrl) {
45
49
  out = out.replace(/href="(\/[^"]*)"/g, (_, path) => {
46
50
  return `href="${resolveStaticUrl(path, baseUrl)}"`;
@@ -1,4 +1,4 @@
1
- export declare const OPENPEEPS_MARKDOWN_PROSE_CLASS = "allpeep-markdown prose prose-sm max-w-none break-words dark:prose-invert prose-a:text-primary prose-a:no-underline hover:prose-a:underline prose-code:before:content-[''] prose-code:after:content-[''] prose-pre:max-w-full prose-pre:overflow-x-auto prose-table:text-base";
2
- export declare const OPENPEEPS_MARKDOWN_STATIC_CLASS = "allpeep-markdown text-sm leading-relaxed break-words text-foreground";
1
+ export declare const OPENPEEPS_MARKDOWN_PROSE_CLASS = "allpeep-markdown prose prose-sm max-w-none break-words dark:prose-invert prose-a:text-primary prose-a:no-underline hover:prose-a:underline prose-code:before:content-[''] prose-code:after:content-[''] prose-pre:max-w-full prose-pre:overflow-x-auto prose-table:text-base prose-iframe:max-w-full prose-iframe:w-full prose-iframe:border-0";
2
+ export declare const OPENPEEPS_MARKDOWN_STATIC_CLASS = "allpeep-markdown text-sm leading-relaxed break-words text-foreground prose-iframe:max-w-full prose-iframe:w-full prose-iframe:border-0";
3
3
  /** Post-card caption text (feed Note/Poll/Article bodies) — Figma "PostCards" postCaption uses base (16px), not the prose-sm default used elsewhere (jam chat, editor preview, full event page). */
4
- export declare const OPENPEEPS_POST_CAPTION_CLASS = "allpeep-markdown prose max-w-none break-words dark:prose-invert prose-a:text-primary prose-a:no-underline hover:prose-a:underline prose-code:before:content-[''] prose-code:after:content-[''] prose-pre:max-w-full prose-pre:overflow-x-auto prose-table:text-base";
4
+ export declare const OPENPEEPS_POST_CAPTION_CLASS = "allpeep-markdown prose max-w-none break-words dark:prose-invert prose-a:text-primary prose-a:no-underline hover:prose-a:underline prose-code:before:content-[''] prose-code:after:content-[''] prose-pre:max-w-full prose-pre:overflow-x-auto prose-table:text-base prose-iframe:max-w-full prose-iframe:w-full prose-iframe:border-0";
@@ -1,4 +1,4 @@
1
- export const OPENPEEPS_MARKDOWN_PROSE_CLASS = "allpeep-markdown prose prose-sm max-w-none break-words dark:prose-invert prose-a:text-primary prose-a:no-underline hover:prose-a:underline prose-code:before:content-[''] prose-code:after:content-[''] prose-pre:max-w-full prose-pre:overflow-x-auto prose-table:text-base";
2
- export const OPENPEEPS_MARKDOWN_STATIC_CLASS = 'allpeep-markdown text-sm leading-relaxed break-words text-foreground';
1
+ export const OPENPEEPS_MARKDOWN_PROSE_CLASS = "allpeep-markdown prose prose-sm max-w-none break-words dark:prose-invert prose-a:text-primary prose-a:no-underline hover:prose-a:underline prose-code:before:content-[''] prose-code:after:content-[''] prose-pre:max-w-full prose-pre:overflow-x-auto prose-table:text-base prose-iframe:max-w-full prose-iframe:w-full prose-iframe:border-0";
2
+ export const OPENPEEPS_MARKDOWN_STATIC_CLASS = 'allpeep-markdown text-sm leading-relaxed break-words text-foreground prose-iframe:max-w-full prose-iframe:w-full prose-iframe:border-0';
3
3
  /** Post-card caption text (feed Note/Poll/Article bodies) — Figma "PostCards" postCaption uses base (16px), not the prose-sm default used elsewhere (jam chat, editor preview, full event page). */
4
- export const OPENPEEPS_POST_CAPTION_CLASS = "allpeep-markdown prose max-w-none break-words dark:prose-invert prose-a:text-primary prose-a:no-underline hover:prose-a:underline prose-code:before:content-[''] prose-code:after:content-[''] prose-pre:max-w-full prose-pre:overflow-x-auto prose-table:text-base";
4
+ export const OPENPEEPS_POST_CAPTION_CLASS = "allpeep-markdown prose max-w-none break-words dark:prose-invert prose-a:text-primary prose-a:no-underline hover:prose-a:underline prose-code:before:content-[''] prose-code:after:content-[''] prose-pre:max-w-full prose-pre:overflow-x-auto prose-table:text-base prose-iframe:max-w-full prose-iframe:w-full prose-iframe:border-0";
@@ -1,5 +1,7 @@
1
1
  export type CompileMarkdownOptions = {
2
2
  origin?: string;
3
- newTab?: boolean;
3
+ /** When true (default), the compiled HTML is sanitized before output. */
4
+ newTab?: boolean | ((link: string) => boolean);
5
+ sanitize?: boolean;
4
6
  };
5
7
  export declare const compileMarkdownToHtml: (source: string, options?: CompileMarkdownOptions) => string;
@@ -1,5 +1,6 @@
1
1
  import { marked, Renderer } from 'marked';
2
- import { linkOpensInNewTab } from './linkTargets.js';
2
+ import DOMPurify from 'dompurify';
3
+ import { isExternalLink, injectIframeSafetyAttrs, MARKDOWN_HTML_ALLOWED_TAGS, MARKDOWN_HTML_ALLOWED_ATTRS, MARKDOWN_HTML_FORBIDDEN_TAGS, MARKDOWN_HTML_FORBIDDEN_ATTRS, } from '@openpeepshq/common/lib';
3
4
  const escapeHtml = (text) => text
4
5
  .replace(/&/g, '&')
5
6
  .replace(/</g, '&lt;')
@@ -13,8 +14,13 @@ const cleanHref = (href) => {
13
14
  return null;
14
15
  }
15
16
  };
17
+ const resolveNewTabFlag = (newTab, href) => {
18
+ if (typeof newTab === 'function')
19
+ return newTab(href);
20
+ return newTab ?? false;
21
+ };
16
22
  export const compileMarkdownToHtml = (source, options = {}) => {
17
- const { origin, newTab = false } = options;
23
+ const { origin, newTab = false, sanitize: shouldSanitize = true } = options;
18
24
  const renderer = new Renderer();
19
25
  renderer.link = function ({ href, title, tokens }) {
20
26
  const text = this.parser.parseInline(tokens);
@@ -22,15 +28,34 @@ export const compileMarkdownToHtml = (source, options = {}) => {
22
28
  if (!cleaned) {
23
29
  return text;
24
30
  }
31
+ const opensNewTab = resolveNewTabFlag(newTab, cleaned);
25
32
  let out = `<a href="${cleaned}"`;
26
33
  if (title) {
27
34
  out += ` title="${escapeHtml(title)}"`;
28
35
  }
29
- if (linkOpensInNewTab(cleaned, origin, newTab)) {
36
+ if (isExternalLink(cleaned, origin) || opensNewTab) {
30
37
  out += ' target="_blank" rel="noopener noreferrer"';
31
38
  }
32
39
  out += `>${text}</a>`;
33
40
  return out;
34
41
  };
35
- return marked.parse(source, { renderer, async: false });
42
+ let html = marked.parse(source, { renderer, async: false });
43
+ if (shouldSanitize) {
44
+ // DOMPurify requires a DOM and is effectively browser-only: in Node
45
+ // (build-time docs manifest, server-side emails) DOMPurify.sanitize is
46
+ // not a function, so skip it. That HTML is re-sanitized at render time
47
+ // via CompiledMarkdown in the browser.
48
+ if (typeof DOMPurify.sanitize === 'function') {
49
+ html = DOMPurify.sanitize(html, {
50
+ ALLOWED_TAGS: [...MARKDOWN_HTML_ALLOWED_TAGS],
51
+ ALLOWED_ATTR: [...MARKDOWN_HTML_ALLOWED_ATTRS],
52
+ FORBID_TAGS: [...MARKDOWN_HTML_FORBIDDEN_TAGS],
53
+ FORBID_ATTR: [...MARKDOWN_HTML_FORBIDDEN_ATTRS],
54
+ });
55
+ }
56
+ // Inject sandbox/loading/containment attrs on iframes after sanitization
57
+ // so attackers can't smuggle attributes through DOMPurify's allowlist.
58
+ html = injectIframeSafetyAttrs(html);
59
+ }
60
+ return html;
36
61
  };
@@ -1,7 +1,9 @@
1
1
  export { OpenpeepsMarkdown } from './OpenpeepsMarkdown';
2
- export type { OpenpeepsMarkdownProps } from './OpenpeepsMarkdown';
2
+ export type { OpenpeepsMarkdownProps, MarkdownMention, } from './OpenpeepsMarkdown';
3
+ export type { MarkdownRenderProps } from '@openpeepshq/common/lib';
3
4
  export { OPENPEEPS_MARKDOWN_PROSE_CLASS, OPENPEEPS_MARKDOWN_STATIC_CLASS, OPENPEEPS_POST_CAPTION_CLASS, } from './classes';
4
5
  export { compileMarkdownToHtml } from './compileMarkdown';
6
+ export type { CompileMarkdownOptions } from './compileMarkdown';
5
7
  export { CompiledMarkdown } from './CompiledMarkdown';
6
8
  export type { CompiledMarkdownProps } from './CompiledMarkdown';
7
9
  export { StaticRenderContext, useStaticRender, resolveStaticUrl, } from './staticRender';
@@ -1,3 +1,2 @@
1
- /** Mirrors Svelte `OpenpeepsMarkdown` / `a.svelte` external-link detection. */
2
- export declare const isExternalLink: (href: string, origin?: string) => boolean;
3
- export declare const linkOpensInNewTab: (href: string | null | undefined, origin: string | undefined, newTab: boolean) => boolean;
1
+ /** Re-exports shared link utilities from @openpeepshq/common. */
2
+ export { isExternalLink } from '@openpeepshq/common/lib';
@@ -1,27 +1,2 @@
1
- /** Mirrors Svelte `OpenpeepsMarkdown` / `a.svelte` external-link detection. */
2
- export const isExternalLink = (href, origin) => {
3
- if (!href || /^(mailto:|tel:|#)/.test(href)) {
4
- return false;
5
- }
6
- if (href.startsWith('/') && !href.startsWith('//')) {
7
- return false;
8
- }
9
- if (!origin) {
10
- return !href.startsWith('/');
11
- }
12
- try {
13
- return new URL(href, origin).origin !== origin;
14
- }
15
- catch {
16
- return !href.startsWith('/');
17
- }
18
- };
19
- export const linkOpensInNewTab = (href, origin, newTab) => {
20
- if (!href) {
21
- return false;
22
- }
23
- if (newTab) {
24
- return true;
25
- }
26
- return isExternalLink(href, origin);
27
- };
1
+ /** Re-exports shared link utilities from @openpeepshq/common. */
2
+ export { isExternalLink } from '@openpeepshq/common/lib';
@@ -1,5 +1,5 @@
1
1
  import { useEffect } from 'react';
2
- import { isExternalLink } from '../components/markdown/linkTargets.js';
2
+ import { isExternalLink } from '@openpeepshq/common/lib';
3
3
  import { useOpenpeeps } from '../contexts/openpeeps/index.js';
4
4
  import { useOptionalPathname } from '../contexts/router/index.js';
5
5
  const PAGE_DEDUPE_MS = 2_000;
@@ -17,6 +17,8 @@ export * from './lib/groupFormErrors';
17
17
  export * from './lib/passwordStrength';
18
18
  export { apiErrorMessage } from './lib/apiErrorMessage';
19
19
  export { performLogin, performRegister, performRequestResetPassword, performResetPassword, performLogout, } from './lib/auth';
20
+ export type { MarkdownRenderProps, MentionProfileRef, } from '@openpeepshq/common/lib';
21
+ export { preprocessMarkdownSource, linkProfileMentions, isExternalLink, toGotoUrl, } from '@openpeepshq/common/lib';
20
22
  export * from './hooks';
21
23
  export { useDefaultVisibility } from './components/post/visibility';
22
24
  export { IdentityContext, useIdentity, useCurrentProfile, useCurrentAccount, useCurrentProfileSettings, useAuthData, } from './components/layout/IdentityContext';
@@ -17,6 +17,7 @@ export * from './lib/groupFormErrors.js';
17
17
  export * from './lib/passwordStrength.js';
18
18
  export { apiErrorMessage } from './lib/apiErrorMessage.js';
19
19
  export { performLogin, performRegister, performRequestResetPassword, performResetPassword, performLogout, } from './lib/auth.js';
20
+ export { preprocessMarkdownSource, linkProfileMentions, isExternalLink, toGotoUrl, } from '@openpeepshq/common/lib';
20
21
  export * from './hooks/index.js';
21
22
  export { useDefaultVisibility } from './components/post/visibility.js';
22
23
  export { IdentityContext, useIdentity, useCurrentProfile, useCurrentAccount, useCurrentProfileSettings, useAuthData, } from './components/layout/IdentityContext.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpeepshq/react",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -34,6 +34,7 @@
34
34
  "@livekit/components-styles": "^1.1.6",
35
35
  "@tanstack/react-query": "5.90.16",
36
36
  "@videojs/react": "^10.0.0-beta.24",
37
+ "dompurify": "^3.4.15",
37
38
  "emoji-picker-element": "^1.29.1",
38
39
  "heic2any": "^0.0.4",
39
40
  "i18next": "^24.2.3",
@@ -51,9 +52,9 @@
51
52
  "simple-icons": "^16.31.0",
52
53
  "uuidv7": "^1.0.2",
53
54
  "@openpeepshq/client": "^0.2.0",
54
- "@openpeepshq/common": "^0.2.0",
55
- "@openpeepshq/react-ui": "^0.3.0",
56
- "@openpeepshq/greenscreen": "^0.2.0"
55
+ "@openpeepshq/react-ui": "^0.4.0",
56
+ "@openpeepshq/greenscreen": "^0.2.0",
57
+ "@openpeepshq/common": "^0.3.0"
57
58
  },
58
59
  "peerDependencies": {
59
60
  "react": "^18.3.0 || ^19.0.0",
@@ -1,4 +1,13 @@
1
+ import { useMemo } from 'react';
1
2
  import type { MouseEvent } from 'react';
3
+ import DOMPurify from 'dompurify';
4
+ import {
5
+ injectIframeSafetyAttrs,
6
+ MARKDOWN_HTML_ALLOWED_TAGS,
7
+ MARKDOWN_HTML_ALLOWED_ATTRS,
8
+ MARKDOWN_HTML_FORBIDDEN_TAGS,
9
+ MARKDOWN_HTML_FORBIDDEN_ATTRS,
10
+ } from '@openpeepshq/common/lib';
2
11
  import { OPENPEEPS_MARKDOWN_PROSE_CLASS } from './classes';
3
12
  import { isExternalLink } from './linkTargets';
4
13
  import { recordOutboundClick } from '../../lib/analyticsClicks';
@@ -6,6 +15,8 @@ import { recordOutboundClick } from '../../lib/analyticsClicks';
6
15
  export interface CompiledMarkdownProps {
7
16
  html: string;
8
17
  className?: string;
18
+ /** When false, HTML is rendered as-is without sanitization (for trusted sources). */
19
+ sanitize?: boolean;
9
20
  }
10
21
 
11
22
  const onMarkdownClick = (event: MouseEvent<HTMLDivElement>) => {
@@ -20,13 +31,31 @@ const onMarkdownClick = (event: MouseEvent<HTMLDivElement>) => {
20
31
  }
21
32
  };
22
33
 
34
+ const sanitizeHtml = (html: string): string => {
35
+ let clean = DOMPurify.sanitize(html, {
36
+ ALLOWED_TAGS: [...MARKDOWN_HTML_ALLOWED_TAGS],
37
+ ALLOWED_ATTR: [...MARKDOWN_HTML_ALLOWED_ATTRS],
38
+ FORBID_TAGS: [...MARKDOWN_HTML_FORBIDDEN_TAGS],
39
+ FORBID_ATTR: [...MARKDOWN_HTML_FORBIDDEN_ATTRS],
40
+ });
41
+ clean = injectIframeSafetyAttrs(clean);
42
+ return clean;
43
+ };
44
+
23
45
  export const CompiledMarkdown = ({
24
46
  html,
25
47
  className,
26
- }: CompiledMarkdownProps) => (
27
- <div
28
- className={className ?? OPENPEEPS_MARKDOWN_PROSE_CLASS}
29
- dangerouslySetInnerHTML={{ __html: html }}
30
- onClick={onMarkdownClick}
31
- />
32
- );
48
+ sanitize = true,
49
+ }: CompiledMarkdownProps) => {
50
+ const safeHtml = useMemo(
51
+ () => (sanitize ? sanitizeHtml(html) : html),
52
+ [html, sanitize],
53
+ );
54
+ return (
55
+ <div
56
+ className={className ?? OPENPEEPS_MARKDOWN_PROSE_CLASS}
57
+ dangerouslySetInnerHTML={{ __html: safeHtml }}
58
+ onClick={onMarkdownClick}
59
+ />
60
+ );
61
+ };
@@ -1,5 +1,6 @@
1
1
  import { useMemo } from 'react';
2
- import { linkProfileMentions } from '@openpeepshq/common/lib';
2
+ import { preprocessMarkdownSource } from '@openpeepshq/common/lib';
3
+ import type { MarkdownRenderProps } from '@openpeepshq/common/lib';
3
4
  import { extractUrlsFromText, isEmail } from '../preview-link/helpers';
4
5
  import { PreviewLink } from '../preview-link/PreviewLink';
5
6
  import {
@@ -9,34 +10,29 @@ import {
9
10
  import { compileMarkdownToHtml } from './compileMarkdown';
10
11
  import { resolveStaticUrl, useStaticRender } from './staticRender';
11
12
 
13
+ /** @deprecated Use {@link MentionProfileRef} from @openpeepshq/common. */
12
14
  export type MarkdownMention = {
13
- text?: string;
14
15
  profile: { handle: string; displayName?: string | null };
15
16
  };
16
17
 
17
- export interface OpenpeepsMarkdownProps {
18
- source?: string;
19
- mentions?: MarkdownMention[];
18
+ export interface OpenpeepsMarkdownProps extends MarkdownRenderProps {
20
19
  className?: string;
21
- linkPreviewMode?: 'prepend' | 'append' | 'none';
22
- /** When true, all links open in a new tab. External links always do. */
23
- newTab?: boolean;
24
20
  }
25
21
 
26
- const hashtagPattern = /(^|[\s(>])#([a-z0-9_]+)(?=\s|$|[.,!?;:])/gi;
22
+ const hashtagPattern = /(^|[\s(>])#([a-z0-9_]+)(?=\s|$|[.,!?;:])$/gi;
27
23
 
28
- function preprocessSource(
24
+ const preprocessWithHashtags = (
29
25
  source: string,
30
- mentions: MarkdownMention[] = [],
31
- ): string {
32
- let text = linkProfileMentions(source, mentions);
26
+ mentions: MarkdownRenderProps['mentions'] = [],
27
+ ): string => {
28
+ let text = preprocessMarkdownSource(source, mentions);
33
29
  text = text.replace(
34
30
  hashtagPattern,
35
31
  (_, prefix: string, tag: string) =>
36
32
  `${prefix}[#${tag}](/tags/${encodeURIComponent(tag)})`,
37
33
  );
38
34
  return text;
39
- }
35
+ };
40
36
 
41
37
  const markdownOrigin = (
42
38
  staticRender: boolean,
@@ -75,8 +71,12 @@ export function OpenpeepsMarkdown({
75
71
  }, [source]);
76
72
 
77
73
  const html = useMemo(() => {
78
- const processed = preprocessSource(source ?? '', mentions);
79
- let out = compileMarkdownToHtml(processed, { origin, newTab });
74
+ const processed = preprocessWithHashtags(source ?? '', mentions);
75
+ let out = compileMarkdownToHtml(processed, {
76
+ origin,
77
+ newTab,
78
+ sanitize: true,
79
+ });
80
80
  if (staticRender && baseUrl) {
81
81
  out = out.replace(/href="(\/[^"]*)"/g, (_, path: string) => {
82
82
  return `href="${resolveStaticUrl(path, baseUrl)}"`;
@@ -1,9 +1,9 @@
1
1
  export const OPENPEEPS_MARKDOWN_PROSE_CLASS =
2
- "allpeep-markdown prose prose-sm max-w-none break-words dark:prose-invert prose-a:text-primary prose-a:no-underline hover:prose-a:underline prose-code:before:content-[''] prose-code:after:content-[''] prose-pre:max-w-full prose-pre:overflow-x-auto prose-table:text-base";
2
+ "allpeep-markdown prose prose-sm max-w-none break-words dark:prose-invert prose-a:text-primary prose-a:no-underline hover:prose-a:underline prose-code:before:content-[''] prose-code:after:content-[''] prose-pre:max-w-full prose-pre:overflow-x-auto prose-table:text-base prose-iframe:max-w-full prose-iframe:w-full prose-iframe:border-0";
3
3
 
4
4
  export const OPENPEEPS_MARKDOWN_STATIC_CLASS =
5
- 'allpeep-markdown text-sm leading-relaxed break-words text-foreground';
5
+ 'allpeep-markdown text-sm leading-relaxed break-words text-foreground prose-iframe:max-w-full prose-iframe:w-full prose-iframe:border-0';
6
6
 
7
7
  /** Post-card caption text (feed Note/Poll/Article bodies) — Figma "PostCards" postCaption uses base (16px), not the prose-sm default used elsewhere (jam chat, editor preview, full event page). */
8
8
  export const OPENPEEPS_POST_CAPTION_CLASS =
9
- "allpeep-markdown prose max-w-none break-words dark:prose-invert prose-a:text-primary prose-a:no-underline hover:prose-a:underline prose-code:before:content-[''] prose-code:after:content-[''] prose-pre:max-w-full prose-pre:overflow-x-auto prose-table:text-base";
9
+ "allpeep-markdown prose max-w-none break-words dark:prose-invert prose-a:text-primary prose-a:no-underline hover:prose-a:underline prose-code:before:content-[''] prose-code:after:content-[''] prose-pre:max-w-full prose-pre:overflow-x-auto prose-table:text-base prose-iframe:max-w-full prose-iframe:w-full prose-iframe:border-0";
@@ -1,9 +1,19 @@
1
1
  import { marked, Renderer, type Tokens } from 'marked';
2
- import { linkOpensInNewTab } from './linkTargets';
2
+ import DOMPurify from 'dompurify';
3
+ import {
4
+ isExternalLink,
5
+ injectIframeSafetyAttrs,
6
+ MARKDOWN_HTML_ALLOWED_TAGS,
7
+ MARKDOWN_HTML_ALLOWED_ATTRS,
8
+ MARKDOWN_HTML_FORBIDDEN_TAGS,
9
+ MARKDOWN_HTML_FORBIDDEN_ATTRS,
10
+ } from '@openpeepshq/common/lib';
3
11
 
4
12
  export type CompileMarkdownOptions = {
5
13
  origin?: string;
6
- newTab?: boolean;
14
+ /** When true (default), the compiled HTML is sanitized before output. */
15
+ newTab?: boolean | ((link: string) => boolean);
16
+ sanitize?: boolean;
7
17
  };
8
18
 
9
19
  const escapeHtml = (text: string): string =>
@@ -21,11 +31,19 @@ const cleanHref = (href: string): string | null => {
21
31
  }
22
32
  };
23
33
 
34
+ const resolveNewTabFlag = (
35
+ newTab: boolean | ((link: string) => boolean) | undefined,
36
+ href: string,
37
+ ): boolean => {
38
+ if (typeof newTab === 'function') return newTab(href);
39
+ return newTab ?? false;
40
+ };
41
+
24
42
  export const compileMarkdownToHtml = (
25
43
  source: string,
26
44
  options: CompileMarkdownOptions = {},
27
45
  ): string => {
28
- const { origin, newTab = false } = options;
46
+ const { origin, newTab = false, sanitize: shouldSanitize = true } = options;
29
47
  const renderer = new Renderer();
30
48
 
31
49
  renderer.link = function (
@@ -38,16 +56,38 @@ export const compileMarkdownToHtml = (
38
56
  return text;
39
57
  }
40
58
 
59
+ const opensNewTab = resolveNewTabFlag(newTab, cleaned);
60
+
41
61
  let out = `<a href="${cleaned}"`;
42
62
  if (title) {
43
63
  out += ` title="${escapeHtml(title)}"`;
44
64
  }
45
- if (linkOpensInNewTab(cleaned, origin, newTab)) {
65
+ if (isExternalLink(cleaned, origin) || opensNewTab) {
46
66
  out += ' target="_blank" rel="noopener noreferrer"';
47
67
  }
48
68
  out += `>${text}</a>`;
49
69
  return out;
50
70
  };
51
71
 
52
- return marked.parse(source, { renderer, async: false }) as string;
72
+ let html = marked.parse(source, { renderer, async: false }) as string;
73
+
74
+ if (shouldSanitize) {
75
+ // DOMPurify requires a DOM and is effectively browser-only: in Node
76
+ // (build-time docs manifest, server-side emails) DOMPurify.sanitize is
77
+ // not a function, so skip it. That HTML is re-sanitized at render time
78
+ // via CompiledMarkdown in the browser.
79
+ if (typeof DOMPurify.sanitize === 'function') {
80
+ html = DOMPurify.sanitize(html, {
81
+ ALLOWED_TAGS: [...MARKDOWN_HTML_ALLOWED_TAGS],
82
+ ALLOWED_ATTR: [...MARKDOWN_HTML_ALLOWED_ATTRS],
83
+ FORBID_TAGS: [...MARKDOWN_HTML_FORBIDDEN_TAGS],
84
+ FORBID_ATTR: [...MARKDOWN_HTML_FORBIDDEN_ATTRS],
85
+ });
86
+ }
87
+ // Inject sandbox/loading/containment attrs on iframes after sanitization
88
+ // so attackers can't smuggle attributes through DOMPurify's allowlist.
89
+ html = injectIframeSafetyAttrs(html);
90
+ }
91
+
92
+ return html;
53
93
  };
@@ -1,11 +1,16 @@
1
1
  export { OpenpeepsMarkdown } from './OpenpeepsMarkdown';
2
- export type { OpenpeepsMarkdownProps } from './OpenpeepsMarkdown';
2
+ export type {
3
+ OpenpeepsMarkdownProps,
4
+ MarkdownMention,
5
+ } from './OpenpeepsMarkdown';
6
+ export type { MarkdownRenderProps } from '@openpeepshq/common/lib';
3
7
  export {
4
8
  OPENPEEPS_MARKDOWN_PROSE_CLASS,
5
9
  OPENPEEPS_MARKDOWN_STATIC_CLASS,
6
10
  OPENPEEPS_POST_CAPTION_CLASS,
7
11
  } from './classes';
8
12
  export { compileMarkdownToHtml } from './compileMarkdown';
13
+ export type { CompileMarkdownOptions } from './compileMarkdown';
9
14
  export { CompiledMarkdown } from './CompiledMarkdown';
10
15
  export type { CompiledMarkdownProps } from './CompiledMarkdown';
11
16
  export {
@@ -1,31 +1,2 @@
1
- /** Mirrors Svelte `OpenpeepsMarkdown` / `a.svelte` external-link detection. */
2
- export const isExternalLink = (href: string, origin?: string): boolean => {
3
- if (!href || /^(mailto:|tel:|#)/.test(href)) {
4
- return false;
5
- }
6
- if (href.startsWith('/') && !href.startsWith('//')) {
7
- return false;
8
- }
9
- if (!origin) {
10
- return !href.startsWith('/');
11
- }
12
- try {
13
- return new URL(href, origin).origin !== origin;
14
- } catch {
15
- return !href.startsWith('/');
16
- }
17
- };
18
-
19
- export const linkOpensInNewTab = (
20
- href: string | null | undefined,
21
- origin: string | undefined,
22
- newTab: boolean,
23
- ): boolean => {
24
- if (!href) {
25
- return false;
26
- }
27
- if (newTab) {
28
- return true;
29
- }
30
- return isExternalLink(href, origin);
31
- };
1
+ /** Re-exports shared link utilities from @openpeepshq/common. */
2
+ export { isExternalLink } from '@openpeepshq/common/lib';
@@ -1,5 +1,5 @@
1
1
  import { useEffect } from 'react';
2
- import { isExternalLink } from '../components/markdown/linkTargets';
2
+ import { isExternalLink } from '@openpeepshq/common/lib';
3
3
  import { useOpenpeeps } from '../contexts/openpeeps';
4
4
  import { useOptionalPathname } from '../contexts/router';
5
5
 
@@ -27,6 +27,17 @@ export {
27
27
  performResetPassword,
28
28
  performLogout,
29
29
  } from './lib/auth';
30
+ export type {
31
+ MarkdownRenderProps,
32
+ MentionProfileRef,
33
+ } from '@openpeepshq/common/lib';
34
+ export {
35
+ preprocessMarkdownSource,
36
+ linkProfileMentions,
37
+ isExternalLink,
38
+ toGotoUrl,
39
+ } from '@openpeepshq/common/lib';
40
+
30
41
  export * from './hooks';
31
42
  export { useDefaultVisibility } from './components/post/visibility';
32
43
  export {