summaryrefslogtreecommitdiff
path: root/src/helper/populate-recents.ts
blob: 551434b372a6411c502a5e6744f532440c32fc9d (plain) (blame)
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
/*** UTILITY ------------------------------------------ ***/

import { remarkRegex } from "src/utility/constant.ts";

/*** EXPORT ------------------------------------------- ***/

export default (documentList: Array<{ filename: string; }>, currentDocument: string): string => {
  const currentIndex = documentList.findIndex(item => item.filename === currentDocument);
  let isMemo = true;
  let maximumIndex = documentList.length;
  let minimumIndex = 0;

  if (remarkRegex.test(currentDocument))
    isMemo = false;

  if (currentIndex < 7)
    minimumIndex = 0;
  else
    minimumIndex = currentIndex - 7;

  if (currentIndex < (maximumIndex - 8))
    maximumIndex = currentIndex + 8;

  const documents = documentList.slice(minimumIndex, maximumIndex).map((arrayItem: { filename: string; }) => {
    if (!arrayItem)
      return "";

    const { filename } = arrayItem;

    if (filename === currentDocument) {
      if (isMemo)
        return `<a class="current" href="/${filename.replace(".txt", "")}">${filename.replace(".txt", "")}</a>`;
      else
        return `<a class="current" href="/remarks/${filename.replace(".txt", "")}">${filename.replace(".txt", "")}</a>`;
    } else {
      if (isMemo)
        return `<a href="/${filename.replace(".txt", "")}">${filename.replace(".txt", "")}</a>`;
      else
        return `<a href="/remarks/${filename.replace(".txt", "")}">${filename.replace(".txt", "")}</a>`;
    }
  });

  return documents.join("");
}