orz-markdown 1.2.0 → 1.2.1

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 +1 @@
1
- {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB,QAkZhC,CAAC;AAEF,wBAAgB,uBAAuB,IAAI,MAAM,CAEhD"}
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB,QA8chC,CAAC;AAEF,wBAAgB,uBAAuB,IAAI,MAAM,CAEhD"}
package/dist/runtime.js CHANGED
@@ -178,11 +178,10 @@ exports.browserRuntimeScript = String.raw `
178
178
  return '---';
179
179
  }
180
180
 
181
- function rt_table(tbl) {
181
+ function rt_tableRows(headRow, bodyRows) {
182
182
  var rows = [];
183
- var aligns = [];
184
183
  var header = [];
185
- var headRow = tbl.querySelector ? tbl.querySelector('thead tr') : null;
184
+ var aligns = [];
186
185
  if (headRow) {
187
186
  var hc = headRow.children;
188
187
  for (var i = 0; i < hc.length; i++) { header.push(rt_inlineChildren(hc[i]).trim()); aligns.push(rt_align(hc[i])); }
@@ -191,7 +190,6 @@ exports.browserRuntimeScript = String.raw `
191
190
  var seps = [];
192
191
  for (var s = 0; s < header.length; s++) seps.push(rt_sep(aligns[s]));
193
192
  rows.push('| ' + seps.join(' | ') + ' |');
194
- var bodyRows = tbl.querySelectorAll ? tbl.querySelectorAll('tbody tr') : [];
195
193
  for (var r = 0; r < bodyRows.length; r++) {
196
194
  var cells = [];
197
195
  var tds = bodyRows[r].children;
@@ -201,6 +199,27 @@ exports.browserRuntimeScript = String.raw `
201
199
  return rows.join('\n');
202
200
  }
203
201
 
202
+ function rt_table(tbl) {
203
+ var headRow = tbl.querySelector ? tbl.querySelector('thead tr') : null;
204
+ var bodyRows = tbl.querySelectorAll ? tbl.querySelectorAll('tbody tr') : [];
205
+ return rt_tableRows(headRow, bodyRows);
206
+ }
207
+
208
+ // Reconstruct a table from bare table-internal nodes (thead/tbody/tr), e.g.
209
+ // when a selection inside a table loses its <table> wrapper.
210
+ function rt_tableFromParts(parts) {
211
+ var headRow = null;
212
+ var bodyRows = [];
213
+ for (var i = 0; i < parts.length; i++) {
214
+ var p = parts[i];
215
+ var tag = p.tagName.toLowerCase();
216
+ if (tag === 'thead') { var tr = p.querySelector ? p.querySelector('tr') : null; if (tr && !headRow) headRow = tr; }
217
+ else if (tag === 'tbody') { var trs = p.querySelectorAll ? p.querySelectorAll('tr') : []; for (var j = 0; j < trs.length; j++) bodyRows.push(trs[j]); }
218
+ else if (tag === 'tr') { if (!headRow) headRow = p; else bodyRows.push(p); }
219
+ }
220
+ return rt_tableRows(headRow, bodyRows);
221
+ }
222
+
204
223
  function rt_codeBlock(pre) {
205
224
  var code = pre.querySelector ? (pre.querySelector('code') || pre) : pre;
206
225
  var lang = '';
@@ -275,6 +294,19 @@ exports.browserRuntimeScript = String.raw `
275
294
  parts.push(items.join('\n'));
276
295
  continue;
277
296
  }
297
+ // Bare table-internal nodes (selection inside a table that lost its
298
+ // <table> wrapper) — reconstruct a Markdown table.
299
+ if (k.nodeType === 1 && /^(thead|tbody|tr)$/.test(k.tagName.toLowerCase())) {
300
+ var trows = [];
301
+ while (i < kids.length) {
302
+ var tk = kids[i];
303
+ if (tk.nodeType === 1 && /^(thead|tbody|tr)$/.test(tk.tagName.toLowerCase())) { trows.push(tk); i++; }
304
+ else if (tk.nodeType === 3 && !(tk.nodeValue || '').trim()) { i++; }
305
+ else break;
306
+ }
307
+ parts.push(rt_tableFromParts(trows));
308
+ continue;
309
+ }
278
310
  var s = rt_blockNode(k);
279
311
  if (s && s.replace(/\s/g, '') !== '') parts.push(s.replace(/\s+$/, ''));
280
312
  i++;
@@ -338,15 +370,32 @@ exports.browserRuntimeScript = String.raw `
338
370
  return false;
339
371
  }
340
372
 
373
+ function rt_isCopyRoot(node) {
374
+ return node.nodeType === 1 && node.classList &&
375
+ (node.classList.contains('markdown-body') || rt_attr(node, 'data-orz-copy') != null);
376
+ }
341
377
  function rt_withinCopyRoot(node) {
342
378
  while (node) {
343
- if (node.nodeType === 1 && node.classList &&
344
- (node.classList.contains('markdown-body') || rt_attr(node, 'data-orz-copy') != null)) return true;
379
+ if (rt_isCopyRoot(node)) return true;
345
380
  node = node.parentNode;
346
381
  }
347
382
  return false;
348
383
  }
349
384
 
385
+ // When a selection sits entirely within one table/blockquote/pre, copy that
386
+ // whole block: a partial table/quote/code fragment isn't valid Markdown, and
387
+ // browsers often clone such selections without the wrapping element.
388
+ function rt_promotableBlock(node) {
389
+ while (node && !rt_isCopyRoot(node)) {
390
+ if (node.nodeType === 1) {
391
+ var tag = node.tagName.toLowerCase();
392
+ if (tag === 'table' || tag === 'blockquote' || tag === 'pre') return node;
393
+ }
394
+ node = node.parentNode;
395
+ }
396
+ return null;
397
+ }
398
+
350
399
  function onCopy(event) {
351
400
  if (!global.document) return;
352
401
  var sel = global.getSelection ? global.getSelection()
@@ -354,9 +403,20 @@ exports.browserRuntimeScript = String.raw `
354
403
  if (!sel || sel.rangeCount === 0 || sel.isCollapsed) return;
355
404
  if (rt_isEditable(sel.anchorNode) || rt_isEditable(sel.focusNode)) return;
356
405
  if (!rt_withinCopyRoot(sel.anchorNode) && !rt_withinCopyRoot(sel.focusNode)) return;
357
- var container = global.document.createElement('div');
358
- for (var i = 0; i < sel.rangeCount; i++) container.appendChild(sel.getRangeAt(i).cloneContents());
359
- var md = elementToMarkdown(container);
406
+
407
+ var md;
408
+ var range0 = sel.getRangeAt(0);
409
+ var common = range0.commonAncestorContainer;
410
+ var promoted = rt_promotableBlock(common.nodeType === 1 ? common : common.parentNode);
411
+ if (promoted && sel.rangeCount === 1) {
412
+ var pwrap = global.document.createElement('div');
413
+ pwrap.appendChild(promoted.cloneNode(true));
414
+ md = elementToMarkdown(pwrap);
415
+ } else {
416
+ var container = global.document.createElement('div');
417
+ for (var i = 0; i < sel.rangeCount; i++) container.appendChild(sel.getRangeAt(i).cloneContents());
418
+ md = elementToMarkdown(container);
419
+ }
360
420
  if (!md) return;
361
421
  var cd = event.clipboardData || global.clipboardData;
362
422
  if (cd && cd.setData) {
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":";;;AAoZA,0DAEC;AAtZY,QAAA,oBAAoB,GAAG,MAAM,CAAC,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkZ7C,CAAC;AAEF,SAAgB,uBAAuB;IACrC,OAAO,4BAAoB,CAAC;AAC9B,CAAC"}
1
+ {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":";;;AAgdA,0DAEC;AAldY,QAAA,oBAAoB,GAAG,MAAM,CAAC,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA8c7C,CAAC;AAEF,SAAgB,uBAAuB;IACrC,OAAO,4BAAoB,CAAC;AAC9B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orz-markdown",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Customized markdown-it parser with official and custom plugins",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",