Skip to content

Commit da1c209

Browse files
authored
feat(wysiwyg): allow to disable markdown-it-attrs in yfm preset (#690)
1 parent 6f54ee7 commit da1c209

File tree

15 files changed

+188
-38
lines changed

15 files changed

+188
-38
lines changed

demo/components/Playground.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export type PlaygroundProps = {
7979
onChangeSplitModeEnabled?: (splitModeEnabled: boolean) => void;
8080
directiveSyntax?: DirectiveSyntaxValue;
8181
disabledHTMLBlockModes?: EmbeddingMode[];
82+
disableMarkdownItAttrs?: boolean;
8283
} & Pick<UseMarkdownEditorProps, 'experimental' | 'wysiwygConfig'> &
8384
Pick<
8485
MarkdownEditorViewProps,
@@ -128,6 +129,7 @@ export const Playground = memo<PlaygroundProps>((props) => {
128129
experimental,
129130
directiveSyntax,
130131
disabledHTMLBlockModes,
132+
disableMarkdownItAttrs,
131133
} = props;
132134
const [editorMode, setEditorMode] = useState<MarkdownEditorMode>(initialEditor ?? 'wysiwyg');
133135
const [mdRaw, setMdRaw] = useState<MarkupString>(initial || '');
@@ -146,10 +148,11 @@ export const Playground = memo<PlaygroundProps>((props) => {
146148
breaks={md.breaks}
147149
needToSanitizeHtml={sanitizeHtml}
148150
plugins={getPlugins({directiveSyntax})}
151+
disableMarkdownItAttrs={disableMarkdownItAttrs}
149152
htmlRuntimeConfig={{disabledModes: disabledHTMLBlockModes}}
150153
/>
151154
),
152-
[sanitizeHtml, disabledHTMLBlockModes],
155+
[sanitizeHtml, disabledHTMLBlockModes, disableMarkdownItAttrs],
153156
);
154157

155158
const logger = useMemo(() => new Logger2().nested({env: 'playground'}), []);
@@ -161,6 +164,7 @@ export const Playground = memo<PlaygroundProps>((props) => {
161164
preset: 'full',
162165
wysiwygConfig: {
163166
placeholderOptions: placeholderOptions,
167+
disableMarkdownAttrs: disableMarkdownItAttrs,
164168
extensions: (builder) => {
165169
builder
166170
.use(Math, {
@@ -250,6 +254,7 @@ export const Playground = memo<PlaygroundProps>((props) => {
250254
experimental?.beforeEditorModeChange,
251255
experimental?.prepareRawMarkup,
252256
directiveSyntax,
257+
disableMarkdownItAttrs,
253258
],
254259
);
255260

demo/components/PlaygroundMini.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type PlaygroundMiniProps = Pick<
2424
| 'onChangeSplitModeEnabled'
2525
| 'directiveSyntax'
2626
| 'disabledHTMLBlockModes'
27+
| 'disableMarkdownItAttrs'
2728
> & {withDefaultInitialContent?: boolean};
2829

2930
export const PlaygroundMini = memo<PlaygroundMiniProps>(

demo/components/SplitModePreview.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ const Preview = withMermaid({runtime: MERMAID_RUNTIME})(
2525
);
2626

2727
export type SplitModePreviewProps = {
28-
plugins?: MarkdownIt.PluginSimple[];
28+
plugins: MarkdownIt.PluginSimple[];
2929
getValue: () => MarkupString;
3030
allowHTML?: boolean;
3131
breaks?: boolean;
3232
linkify?: boolean;
3333
linkifyTlds?: string | string[];
3434
needToSanitizeHtml?: boolean;
3535
htmlRuntimeConfig?: HTMLRuntimeConfig;
36+
disableMarkdownItAttrs?: boolean;
3637
};
3738

3839
export const SplitModePreview: React.FC<SplitModePreviewProps> = (props) => {
@@ -45,6 +46,7 @@ export const SplitModePreview: React.FC<SplitModePreviewProps> = (props) => {
4546
linkifyTlds,
4647
needToSanitizeHtml,
4748
htmlRuntimeConfig,
49+
disableMarkdownItAttrs,
4850
} = props;
4951
const [html, setHtml] = useState('');
5052
const [meta, setMeta] = useState<object | undefined>({});
@@ -58,12 +60,17 @@ export const SplitModePreview: React.FC<SplitModePreviewProps> = (props) => {
5860
const res = transform(getValue(), {
5961
allowHTML,
6062
breaks,
61-
plugins,
6263
linkify,
6364
linkifyTlds,
6465
needToSanitizeHtml,
6566
linkAttrs: [[ML_ATTR, true]],
6667
defaultClassName: colorClassName,
68+
plugins: [
69+
...plugins,
70+
...(disableMarkdownItAttrs
71+
? [(md: MarkdownIt) => md.core.ruler.disable('curly_attributes')]
72+
: []),
73+
],
6774
}).result;
6875
setHtml(res.html);
6976
setMeta(res.meta);

demo/defaults/args.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ export const args: Meta<PlaygroundMiniProps>['args'] = {
1818
height: 'initial',
1919
directiveSyntax: 'disabled',
2020
disabledHTMLBlockModes: [],
21+
disableMarkdownItAttrs: true,
2122
};

demo/stories/playground/Playground.stories.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import {type PlaygroundProps, Playground as component} from '../../components/Pl
44
import {args} from '../../defaults/args';
55
import {getInitialMd} from '../../utils/getInitialMd';
66

7-
export const Story: StoryObj<typeof component> = {};
7+
export const Story: StoryObj<typeof component> = {
8+
args: {
9+
disableMarkdownItAttrs: true,
10+
},
11+
};
812
Story.storyName = 'Playground';
913

1014
const meta: Meta<PlaygroundProps> = {

demo/stories/presets/Preset.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export const Preset = memo<PresetDemoProps>((props) => {
9090
splitModeEnabled: true,
9191
},
9292
wysiwygConfig: {
93+
disableMarkdownAttrs: true,
9394
extensionOptions: {
9495
imgSize: {
9596
parseInsertedUrlAsImage,

package-lock.json

Lines changed: 26 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
"@codemirror/search": "~6.5.8",
182182
"@codemirror/state": "~6.5.1",
183183
"@codemirror/view": "~6.36.2",
184+
"@diplodoc/utils": "^2.1.0",
184185
"@gravity-ui/i18n": "^1.7.0",
185186
"@gravity-ui/icons": "^2.12.0",
186187
"@lezer/highlight": "~1.2.1",

src/bundle/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ export type MarkdownEditorWysiwygConfig = {
178178
extensionOptions?: ExtensionsOptions;
179179
escapeConfig?: EscapeConfig;
180180
placeholderOptions?: WysiwygPlaceholderOptions;
181+
// MAJOR: remove markdown-it-attrs
182+
/**
183+
* Disable the markdown-it-attrs plugin in the markup parser.
184+
*
185+
* Note: The use of the markdown-it-attrs plugin will be removed in the next major version.
186+
*/
187+
disableMarkdownAttrs?: boolean;
181188
};
182189

183190
export type MarkdownEditorOptions = {

src/bundle/useMarkdownEditor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export function useMarkdownEditor(
5757
editor.emit('submit', null);
5858
return true;
5959
},
60+
disableMdAttrs: wysiwygConfig.disableMarkdownAttrs,
6061
preserveEmptyRows: experimental.preserveEmptyRows,
6162
placeholderOptions: wysiwygConfig.placeholderOptions,
6263
mdBreaks: md.breaks,

0 commit comments

Comments
 (0)