All files / app/mods/tools archive.ts

26.66% Statements 4/15
0% Branches 0/18
0% Functions 0/2
33.33% Lines 4/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69117x       117x   117x                                                                                                                 117x          
import { DateTime } from 'luxon';
import { Plugin } from '../../model/plugin';
import { Ref } from '../../model/ref';
import { Mod } from '../../model/tag';
import { getHost, getScheme } from '../../util/http';
 
export const archivePlugin: Plugin = {
  tag: 'plugin/archive',
  name: $localize`🗄️ Archive`,
  config: {
    type: 'plugin',
    experimental: true,
    add: true,
    generated: $localize`Generated by jasper-ui ${DateTime.now().toISO()}`,
    icons: [{ label: $localize`🗄️` }],
    filters: [
      { query: 'plugin/archive', label: $localize`🗄️ archive`, title: $localize`Has archive link`, group: $localize`Plugins 🧰️` },
    ],
    advancedActions: [
      { event: 'archive', label: $localize`archive`, global: true, scheme: 'http:' },
      { event: 'archive', label: $localize`archive`, global: true, scheme: 'https:' },
    ],
    hosts: [
      'archive.ph',
      '12ft.io',
    ],
    defaultArchive: 'https://archive.ph/newest/',
    // defaultArchive: 'https://12ft.io/?proxy=',
    advancedForm: [{
      key: 'url',
      type: 'url',
      props: {
        label: $localize`URL:`,
      },
    }],
  },
  defaults: {},
  schema: {
    optionalProperties: {
      url: { type: 'string' },
    },
  },
};
 
export function findArchive(plugin: typeof archivePlugin, ref?: Ref) {
  if (!ref) return '';
  if (ref.alternateUrls && plugin!.config?.hosts) {
    for (const s of ref.alternateUrls) {
      if (plugin!.config.hosts.includes(getHost(s)!)) return s;
    }
  }
  const scheme = getScheme(ref.url);
  if (scheme !== 'http:' && scheme !== 'https:') return '';
  return plugin!.config!.defaultArchive + ref.url;
}
 
export function archiveUrl(plugin: Plugin, ref?: Ref, repost?: Ref) {
  return ref?.plugins?.['plugin/archive']?.url
    || repost?.plugins?.['plugin/archive']?.url
    || findArchive(plugin, ref)
    || findArchive(plugin, repost);
}
 
export const archiveMod: Mod = {
  plugin: [
    archivePlugin,
  ],
};