@milkdown/preset-commonmark 7.15.0 → 7.15.2

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.
@@ -0,0 +1,2 @@
1
+ import '@testing-library/jest-dom/vitest';
2
+ //# sourceMappingURL=html.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"html.spec.d.ts","sourceRoot":"","sources":["../../src/__test__/html.spec.ts"],"names":[],"mappings":"AAAA,OAAO,kCAAkC,CAAA"}
package/lib/index.js CHANGED
@@ -97,7 +97,7 @@ withMeta(emphasisStarInputRule, {
97
97
  group: "Emphasis"
98
98
  });
99
99
  const emphasisUnderscoreInputRule = $inputRule((ctx) => {
100
- return markRule(/(?:^|[^_])_([^_]+)_$/, emphasisSchema.type(ctx), {
100
+ return markRule(/\b_(?![_\s])(.*?[^_\s])_\b/, emphasisSchema.type(ctx), {
101
101
  getAttr: () => ({
102
102
  marker: "_"
103
103
  }),
@@ -187,13 +187,17 @@ withMeta(toggleStrongCommand, {
187
187
  group: "Strong"
188
188
  });
189
189
  const strongInputRule = $inputRule((ctx) => {
190
- return markRule(/(?:\*\*|__)([^*_]+)(?:\*\*|__)$/, strongSchema.type(ctx), {
191
- getAttr: (match) => {
192
- return {
193
- marker: match[0].startsWith("*") ? "*" : "_"
194
- };
190
+ return markRule(
191
+ new RegExp("(?<![\\w:/])(?:\\*\\*|__)([^*_]+?)(?:\\*\\*|__)(?![\\w/])$"),
192
+ strongSchema.type(ctx),
193
+ {
194
+ getAttr: (match) => {
195
+ return {
196
+ marker: match[0].startsWith("*") ? "*" : "_"
197
+ };
198
+ }
195
199
  }
196
- });
200
+ );
197
201
  });
198
202
  withMeta(strongInputRule, {
199
203
  displayName: "InputRule<strong>",
@@ -515,7 +519,7 @@ withMeta(paragraphKeymap.shortcuts, {
515
519
  });
516
520
  const headingIndex = Array(6).fill(0).map((_, i) => i + 1);
517
521
  function defaultHeadingIdGenerator(node) {
518
- return node.textContent.toLowerCase().trim().replace(/[^\w\s-]/g, "").replace(/\s+/g, "-");
522
+ return node.textContent.toLowerCase().trim().replace(/\s+/g, "-");
519
523
  }
520
524
  const headingIdGenerator = $ctx(
521
525
  defaultHeadingIdGenerator,
@@ -1909,12 +1913,13 @@ function flatMapWithDepth(ast, fn) {
1909
1913
  return fn(node, index, parent);
1910
1914
  }
1911
1915
  }
1916
+ const BLOCK_CONTAINER_TYPES = ["root", "blockquote", "listItem"];
1912
1917
  const remarkHtmlTransformer = $remark(
1913
1918
  "remarkHTMLTransformer",
1914
1919
  () => () => (tree) => {
1915
1920
  flatMapWithDepth(tree, (node, _index, parent) => {
1916
1921
  if (!isHTML(node)) return [node];
1917
- if (parent?.type === "root") {
1922
+ if (parent && BLOCK_CONTAINER_TYPES.includes(parent.type)) {
1918
1923
  node.children = [{ ...node }];
1919
1924
  delete node.value;
1920
1925
  node.type = "paragraph";
@@ -2159,12 +2164,14 @@ withMeta(syncHeadingIdPlugin, {
2159
2164
  group: "Prose"
2160
2165
  });
2161
2166
  const syncListOrderPlugin = $prose((ctx) => {
2162
- const syncOrderLabel = (view) => {
2163
- if (view.composing || !view.editable) return;
2167
+ const syncOrderLabel = (transactions, _oldState, newState) => {
2168
+ if (!newState.selection || transactions.some(
2169
+ (tr2) => tr2.getMeta("addToHistory") === false || !tr2.isGeneric
2170
+ ))
2171
+ return null;
2164
2172
  const orderedListType = orderedListSchema.type(ctx);
2165
2173
  const bulletListType = bulletListSchema.type(ctx);
2166
2174
  const listItemType = listItemSchema.type(ctx);
2167
- const state = view.state;
2168
2175
  const handleNodeItem = (attrs, index) => {
2169
2176
  let changed = false;
2170
2177
  const expectedLabel = `${index + 1}.`;
@@ -2174,50 +2181,47 @@ const syncListOrderPlugin = $prose((ctx) => {
2174
2181
  }
2175
2182
  return changed;
2176
2183
  };
2177
- let tr = state.tr;
2184
+ let tr = newState.tr;
2178
2185
  let needDispatch = false;
2179
- state.doc.descendants((node, pos, parent, index) => {
2180
- if (node.type === bulletListType) {
2181
- const base = node.maybeChild(0);
2182
- if (base?.type === listItemType && base.attrs.listType === "ordered") {
2183
- needDispatch = true;
2184
- tr.setNodeMarkup(pos, orderedListType, { spread: "true" });
2185
- node.descendants((child, pos2, _parent, index2) => {
2186
- if (child.type === listItemType) {
2187
- const attrs = { ...child.attrs };
2188
- const changed = handleNodeItem(attrs, index2);
2189
- if (changed) tr = tr.setNodeMarkup(pos2, void 0, attrs);
2190
- }
2191
- return false;
2192
- });
2193
- }
2194
- } else if (node.type === listItemType && parent?.type === orderedListType) {
2195
- const attrs = { ...node.attrs };
2196
- let changed = false;
2197
- if (attrs.listType !== "ordered") {
2198
- attrs.listType = "ordered";
2199
- changed = true;
2200
- }
2201
- const base = parent?.maybeChild(0);
2202
- if (base) changed = handleNodeItem(attrs, index);
2203
- if (changed) {
2204
- tr = tr.setNodeMarkup(pos, void 0, attrs);
2205
- needDispatch = true;
2186
+ newState.doc.descendants(
2187
+ (node, pos, parent, index) => {
2188
+ if (node.type === bulletListType) {
2189
+ const base = node.maybeChild(0);
2190
+ if (base?.type === listItemType && base.attrs.listType === "ordered") {
2191
+ needDispatch = true;
2192
+ tr.setNodeMarkup(pos, orderedListType, { spread: "true" });
2193
+ node.descendants(
2194
+ (child, pos2, _parent, index2) => {
2195
+ if (child.type === listItemType) {
2196
+ const attrs = { ...child.attrs };
2197
+ const changed = handleNodeItem(attrs, index2);
2198
+ if (changed) tr = tr.setNodeMarkup(pos2, void 0, attrs);
2199
+ }
2200
+ return false;
2201
+ }
2202
+ );
2203
+ }
2204
+ } else if (node.type === listItemType && parent?.type === orderedListType) {
2205
+ const attrs = { ...node.attrs };
2206
+ let changed = false;
2207
+ if (attrs.listType !== "ordered") {
2208
+ attrs.listType = "ordered";
2209
+ changed = true;
2210
+ }
2211
+ const base = parent?.maybeChild(0);
2212
+ if (base) changed = handleNodeItem(attrs, index);
2213
+ if (changed) {
2214
+ tr = tr.setNodeMarkup(pos, void 0, attrs);
2215
+ needDispatch = true;
2216
+ }
2206
2217
  }
2207
2218
  }
2208
- });
2209
- if (needDispatch) view.dispatch(tr.setMeta("addToHistory", false));
2219
+ );
2220
+ return needDispatch ? tr.setMeta("addToHistory", false) : null;
2210
2221
  };
2211
2222
  return new Plugin({
2212
2223
  key: new PluginKey("MILKDOWN_KEEP_LIST_ORDER"),
2213
- view: (view) => {
2214
- syncOrderLabel(view);
2215
- return {
2216
- update: (view2) => {
2217
- syncOrderLabel(view2);
2218
- }
2219
- };
2220
- }
2224
+ appendTransaction: syncOrderLabel
2221
2225
  });
2222
2226
  });
2223
2227
  withMeta(syncListOrderPlugin, {