Store and publish annotations on the web, as described in the Web Annotation Discovery proposal.

web-annotation-discovery-se.../routes/ util.ts
31 lines
823 B

  1. // Copyright (c) 2020 Jan Kaßel
  2. // Copyright (c) 2022 Gerben
  3. //
  4. // SPDX-License-Identifier: MIT
  5. import { randomBytes } from 'crypto';
  6. import type { Level } from 'level';
  7. export function generateKey(length = 16) {
  8. return randomBytes(length).toString('hex');
  9. }
  10. export async function getPrefixedEntries(db: Level<string, any>, prefix: string, shallow?: boolean) {
  11. const entries: [string, any][] = [];
  12. for await (const entry of db.iterator({ gt: prefix })) {
  13. if (!entry[0].startsWith(prefix)) break;
  14. if (shallow && entry[0].slice(prefix.length).includes('/')) continue;
  15. entries.push(entry);
  16. }
  17. return entries;
  18. }
  19. export function escapeHtml(s: string) {
  20. return s
  21. .replace('<', '&lt;')
  22. .replace('>', '&gt;')
  23. .replace('&', '&amp;')
  24. .replace('"', '&quot;')
  25. .replace("'", '&apos;');
  26. }