Skip to content

Commit 2676b45

Browse files
authored
feat(bundle): pass md options to renderPreview callback (#435)
1 parent 8499538 commit 2676b45

File tree

6 files changed

+30
-22
lines changed

6 files changed

+30
-22
lines changed

demo/Playground.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,18 @@ export const Playground = React.memo<PlaygroundProps>((props) => {
145145
}, [mdRaw]);
146146

147147
const renderPreview = useCallback<RenderPreview>(
148-
({getValue}) => (
148+
({getValue, md}) => (
149149
<SplitModePreview
150150
getValue={getValue}
151-
allowHTML={allowHTML}
152-
linkify={linkify}
153-
linkifyTlds={linkifyTlds}
154-
breaks={breaks}
151+
allowHTML={md.html}
152+
linkify={md.linkify}
153+
linkifyTlds={md.linkifyTlds}
154+
breaks={md.breaks}
155155
needToSanitizeHtml={sanitizeHtml}
156156
plugins={plugins}
157157
/>
158158
),
159-
[allowHTML, breaks, linkify, linkifyTlds, sanitizeHtml],
159+
[sanitizeHtml],
160160
);
161161

162162
const mdEditor = useMarkdownEditor({

demo/PresetDemo.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,18 @@ export const PresetDemo = React.memo<PresetDemoProps>((props) => {
6464
const [mdRaw, setMdRaw] = React.useState<MarkupString>('');
6565

6666
const renderPreview = useCallback<RenderPreview>(
67-
({getValue}) => (
67+
({getValue, md}) => (
6868
<SplitModePreview
6969
getValue={getValue}
70-
allowHTML={allowHTML}
71-
linkify={linkify}
72-
linkifyTlds={linkifyTlds}
73-
breaks={breaks}
70+
allowHTML={md.html}
71+
linkify={md.linkify}
72+
linkifyTlds={md.linkifyTlds}
73+
breaks={md.breaks}
7474
needToSanitizeHtml
7575
plugins={plugins}
7676
/>
7777
),
78-
[allowHTML, breaks, linkify, linkifyTlds],
78+
[],
7979
);
8080

8181
const mdEditor = useMarkdownEditor({

src/bundle/Editor.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type {FileUploadHandler} from '../utils/upload';
2222
import type {
2323
MarkdownEditorMode as EditorMode,
2424
MarkdownEditorPreset as EditorPreset,
25+
MarkdownEditorMdOptions,
2526
MarkdownEditorOptions,
2627
MarkdownEditorMarkupConfig as MarkupConfig,
2728
RenderPreview,
@@ -77,6 +78,7 @@ export interface EditorInt
7778
readonly splitModeEnabled: boolean;
7879
readonly splitMode: SplitMode;
7980
readonly preset: EditorPreset;
81+
readonly mdOptions: Readonly<MarkdownEditorMdOptions>;
8082

8183
/** @internal used in demo for dev-tools */
8284
readonly _wysiwygView?: PMEditorView;
@@ -132,11 +134,9 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> implements EditorI
132134
#markupEditor?: MarkupEditor;
133135
#markupConfig: MarkupConfig;
134136
#escapeConfig?: EscapeConfig;
137+
#mdOptions: Readonly<MarkdownEditorMdOptions>;
135138

136139
readonly #preset: EditorPreset;
137-
#allowHTML?: boolean;
138-
#linkify?: boolean;
139-
#linkifyTlds?: string | string[];
140140
#extensions?: WysiwygEditorOptions['extensions'];
141141
#renderStorage: ReactRenderStorage;
142142
#fileUploadHandler?: FileUploadHandler;
@@ -209,6 +209,10 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> implements EditorI
209209
return this.#preset;
210210
}
211211

212+
get mdOptions(): Readonly<MarkdownEditorMdOptions> {
213+
return this.#mdOptions;
214+
}
215+
212216
get renderPreview(): RenderPreview | undefined {
213217
return this.#renderPreview;
214218
}
@@ -233,9 +237,9 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> implements EditorI
233237
mdPreset,
234238
initialContent: this.#markup,
235239
extensions: this.#extensions,
236-
allowHTML: this.#allowHTML,
237-
linkify: this.#linkify,
238-
linkifyTlds: this.#linkifyTlds,
240+
allowHTML: this.#mdOptions.html,
241+
linkify: this.#mdOptions.linkify,
242+
linkifyTlds: this.#mdOptions.linkifyTlds,
239243
escapeConfig: this.#escapeConfig,
240244
onChange: () => this.emit('rerender-toolbar', null),
241245
onDocChange: () => this.emit('change', null),
@@ -303,9 +307,7 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> implements EditorI
303307
this.#markup = initial.markup ?? '';
304308

305309
this.#preset = opts.preset ?? 'full';
306-
this.#linkify = md.linkify;
307-
this.#linkifyTlds = md.linkifyTlds;
308-
this.#allowHTML = md.html;
310+
this.#mdOptions = md;
309311
this.#extensions = wysiwygConfig.extensions;
310312
this.#markupConfig = {...opts.markupConfig};
311313

src/bundle/MarkdownEditorView.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ export const MarkdownEditorView = React.forwardRef<HTMLDivElement, MarkdownEdito
239239
{editor.renderPreview?.({
240240
getValue: editor.getValue,
241241
mode: 'preview',
242+
md: editor.mdOptions,
242243
})}
243244
</div>
244245
{settings}

src/bundle/SplitModeView.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ const SplitModeView = React.forwardRef<HTMLDivElement, SplitModeProps>(({editor}
106106
<Label icon={<Eye />} className={b('preview-sign')} size={'m'}>
107107
{i18n('split-mode-text')}
108108
</Label>
109-
{editor.renderPreview?.({getValue: editor.getValue, mode: 'split'})}
109+
{editor.renderPreview?.({
110+
getValue: editor.getValue,
111+
mode: 'split',
112+
md: editor.mdOptions,
113+
})}
110114
</div>
111115
);
112116
});

src/bundle/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type MarkdownEditorSplitMode = false | 'horizontal' | 'vertical';
1818
export type RenderPreviewParams = {
1919
getValue: () => MarkupString;
2020
mode: 'preview' | 'split';
21+
md: Readonly<MarkdownEditorMdOptions>;
2122
};
2223
export type RenderPreview = (params: RenderPreviewParams) => ReactNode;
2324

0 commit comments

Comments
 (0)