nomagick commited on
Commit
cadb2fd
·
unverified ·
1 Parent(s): 7ded2de

feat: provide option to discard links

Browse files
src/dto/turndown-tweakable-options.ts CHANGED
@@ -36,15 +36,15 @@ export class TurnDownTweakableOptions extends AutoCastable {
36
 
37
  @Prop({
38
  desc: 'Turndown options > linkStyle',
39
- type: new Set(['inlined', 'referenced']),
40
  })
41
- linkStyle?: 'inlined' | 'referenced';
42
 
43
  @Prop({
44
  desc: 'Turndown options > linkReferenceStyle',
45
- type: new Set(['full', 'collapsed', 'shortcut']),
46
  })
47
- linkReferenceStyle?: 'full' | 'collapsed' | 'shortcut';
48
 
49
  static fromCtx(ctx: Context, prefix= 'x-md-') {
50
  const draft: Record<string, string> = {};
 
36
 
37
  @Prop({
38
  desc: 'Turndown options > linkStyle',
39
+ type: new Set(['inlined', 'referenced', 'discarded']),
40
  })
41
+ linkStyle?: 'inlined' | 'referenced' | 'discarded';
42
 
43
  @Prop({
44
  desc: 'Turndown options > linkReferenceStyle',
45
+ type: new Set(['full', 'collapsed', 'shortcut', 'discarded']),
46
  })
47
+ linkReferenceStyle?: 'full' | 'collapsed' | 'shortcut' | 'discarded';
48
 
49
  static fromCtx(ctx: Context, prefix= 'x-md-') {
50
  const draft: Record<string, string> = {};
src/services/snapshot-formatter.ts CHANGED
@@ -655,22 +655,37 @@ ${suffixMixins.length ? `\n${suffixMixins.join('\n\n')}\n` : ''}`;
655
  return `${trimmed.replace(/\n{3,}/g, '\n\n')}\n\n`;
656
  }
657
  });
658
- turnDownService.addRule('improved-inline-link', {
659
- filter: function (node, options) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
660
  return Boolean(
661
- options.linkStyle === 'inlined' &&
662
  node.nodeName === 'A' &&
663
  node.getAttribute('href')
664
  );
665
  },
666
 
667
- replacement: function (content, node: any) {
668
- const href = node.getAttribute('href');
669
  let title = cleanAttribute(node.getAttribute('title'));
670
- if (title) title = ' "' + title.replace(/"/g, '\\"') + '"';
671
-
 
672
  const fixedContent = content.replace(/\s+/g, ' ').trim();
673
- let fixedHref = href.replace(/\s+/g, '').trim();
674
  if (options?.url) {
675
  try {
676
  fixedHref = new URL(fixedHref, options.url).toString();
@@ -679,7 +694,46 @@ ${suffixMixins.length ? `\n${suffixMixins.join('\n\n')}\n` : ''}`;
679
  }
680
  }
681
 
682
- return `[${fixedContent}](${fixedHref}${title || ''})`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
683
  }
684
  });
685
  turnDownService.addRule('improved-code', {
 
655
  return `${trimmed.replace(/\n{3,}/g, '\n\n')}\n\n`;
656
  }
657
  });
658
+
659
+ let realLinkStyle: 'inlined' | 'collapsed' | 'shortcut' | 'referenced' | 'discarded' = 'inlined';
660
+ if (turndownOpts?.linkStyle === 'referenced' || turndownOpts?.linkReferenceStyle) {
661
+ realLinkStyle = 'referenced';
662
+ if (turndownOpts?.linkReferenceStyle === 'collapsed') {
663
+ realLinkStyle = 'collapsed';
664
+ } else if (turndownOpts?.linkReferenceStyle === 'shortcut') {
665
+ realLinkStyle = 'shortcut';
666
+ } else if (turndownOpts?.linkReferenceStyle === 'discarded') {
667
+ realLinkStyle = 'discarded';
668
+ }
669
+ } else if (turndownOpts?.linkStyle === 'discarded') {
670
+ realLinkStyle = 'discarded';
671
+ }
672
+
673
+ turnDownService.addRule('improved-link', {
674
+ filter: function (node, _options) {
675
  return Boolean(
 
676
  node.nodeName === 'A' &&
677
  node.getAttribute('href')
678
  );
679
  },
680
 
681
+ replacement: function (this: { references: string[]; }, content, node: any) {
682
+ var href = node.getAttribute('href');
683
  let title = cleanAttribute(node.getAttribute('title'));
684
+ if (title) title = ` "${title.replace(/"/g, '\\"')}"`;
685
+ let replacement;
686
+ let reference;
687
  const fixedContent = content.replace(/\s+/g, ' ').trim();
688
+ let fixedHref = href;
689
  if (options?.url) {
690
  try {
691
  fixedHref = new URL(fixedHref, options.url).toString();
 
694
  }
695
  }
696
 
697
+ switch (realLinkStyle) {
698
+ case 'inlined':
699
+ replacement = `[${fixedContent}](${fixedHref}${title || ''})`;
700
+ reference = undefined;
701
+ break;
702
+ case 'collapsed':
703
+ replacement = `[${fixedContent}][]`;
704
+ reference = `[${fixedContent}]: ${fixedHref}${title}`;
705
+ break;
706
+ case 'shortcut':
707
+ replacement = `[${fixedContent}]`;
708
+ reference = `[${fixedContent}]: ${fixedHref}${title}`;
709
+ break;
710
+ case 'discarded':
711
+ replacement = content;
712
+ reference = undefined;
713
+ break;
714
+ default:
715
+ const id = this.references.length + 1;
716
+ replacement = `[${fixedContent}][${id}]`;
717
+ reference = `[${id}]${fixedHref}${title}`;
718
+ }
719
+
720
+ if (reference) {
721
+ this.references.push(reference);
722
+ }
723
+
724
+ return replacement;
725
+ },
726
+
727
+ // @ts-ignore
728
+ references: [],
729
+
730
+ append: function (this: { references: string[]; }) {
731
+ let references = '';
732
+ if (this.references.length) {
733
+ references = `\n\n${this.references.join('\n')}\n\n`;
734
+ this.references = []; // Reset references
735
+ }
736
+ return references;
737
  }
738
  });
739
  turnDownService.addRule('improved-code', {