aboutsummaryrefslogtreecommitdiff
path: root/_posts/_posts.11tydata.js
blob: 2e07a0e2383b2fea9f4a251636d5ec1abad00029 (plain)
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
69
70
71
72
73
74
75
76
77
import markdownIt from "markdown-it";
const md = markdownIt();

export default {
  layout: "default",
  tags: "post",
  permalink:
    "/{{ page.date | date: '%Y/%m/%d', 'Etc/UTC' }}/{{ page.fileSlug }}.html",
  showDate: true,
  eleventyComputed: {
    description: async (data) => {
      if (data.description !== undefined && data.description !== "")
        return data.description.trim();
      if (data.page.rawInput === undefined) return null;
      const markdownAST = md.parse(data.page.rawInput, {});
      let firstParagraph = "";
      let skippingBlockquote = false;
      let skippingStrikethrough = false;
      extractParagraph: for (const astNode of markdownAST) {
        if (skippingBlockquote) {
          if (astNode.type === "blockquote_close") skippingBlockquote = false;
          continue;
        }
        switch (astNode.type) {
          case "paragraph_open":
            continue;
          case "inline":
            for (const child of astNode.children) {
              if (skippingStrikethrough) {
                if (child.type === "s_close") {
                  skippingStrikethrough = false;
                  // hopefully this is just never wrong
                  if (firstParagraph.endsWith(" ")) {
                    firstParagraph = firstParagraph.replace(/ $/, "");
                  }
                }
                continue;
              }
              switch (child.type) {
                case "text":
                  firstParagraph += child.content;
                  break;
                case "softbreak":
                  firstParagraph += " ";
                  break;
                case "link_open":
                case "link_close":
                case "em_open":
                case "em_close":
                case "strong_open":
                case "strong_close":
                  break;
                case "s_open":
                  skippingStrikethrough = true;
                  break;
                default:
                  console.log(data.page.fileSlug, "inline", child);
              }
            }
            break;
          case "paragraph_close":
            break extractParagraph;
          case "blockquote_open":
            skippingBlockquote = true;
            break;
          case "ordered_list_open":
            throw TypeError(
              `Can't auto-generate description for ${data.page.inputPath} - starts with list`,
            );
          default:
            console.log(data.page.fileSlug, "block", astNode);
        }
      }
      return firstParagraph;
    },
  },
};