@t8/docsgen 0.2.29 → 0.2.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/bin.js CHANGED
@@ -451,7 +451,7 @@ function getTitle(ctx, { cover, originalContent, withPackageURL } = {}) {
451
451
  }
452
452
 
453
453
  // src/bin/parsing/getParsedContent.ts
454
- var import_jsdom4 = require("jsdom");
454
+ var import_jsdom3 = require("jsdom");
455
455
  var import_markdown_it = __toESM(require("markdown-it"));
456
456
 
457
457
  // src/bin/getSlug.ts
@@ -538,26 +538,22 @@ function getSectionPostprocess(linkMap) {
538
538
  };
539
539
  }
540
540
 
541
+ // src/bin/parsing/isBadgeContainer.ts
542
+ var badgeImageSelector = 'img[src^="https://img.shields.io/"], img[src^="https://flat.badgen.net/"]';
543
+ function isBadgeContainer(element) {
544
+ if (!element.matches("p") || element.children.length === 0) return false;
545
+ for (let e of element.children) {
546
+ if (!e.matches(badgeImageSelector) && (!e.matches("a") || e.children.length !== 1 || !e.children[0]?.matches(badgeImageSelector)))
547
+ return false;
548
+ }
549
+ return true;
550
+ }
551
+
541
552
  // src/bin/parsing/joinLines.ts
542
553
  function joinLines(x) {
543
554
  return x.join("\n").trim();
544
555
  }
545
556
 
546
- // src/bin/parsing/postprocessBadges.ts
547
- var import_jsdom3 = require("jsdom");
548
- function postprocessBadges(content) {
549
- let { document } = new import_jsdom3.JSDOM(content).window;
550
- for (let img of document.querySelectorAll("img")) {
551
- let parent = img.parentElement;
552
- if (!parent) continue;
553
- let container = document.createElement("span");
554
- container.className = "badge";
555
- parent.insertBefore(container, img);
556
- container.append(img);
557
- }
558
- return document.body.innerHTML;
559
- }
560
-
561
557
  // src/bin/parsing/preprocessContent.ts
562
558
  var marker = {
563
559
  show: {
@@ -589,9 +585,9 @@ async function getParsedContent(ctx) {
589
585
  getLocation(ctx, "README.md", ctx.source)
590
586
  );
591
587
  let content = md.render(preprocessContent(rawContent));
592
- let dom = new import_jsdom4.JSDOM(content);
588
+ let dom = new import_jsdom3.JSDOM(content);
593
589
  let { nav, linkMap: navLinkMap } = buildNav(ctx, dom);
594
- let badges = [];
590
+ let badges = "";
595
591
  let title = "";
596
592
  let description = [];
597
593
  let descriptionNote = [];
@@ -601,41 +597,50 @@ async function getParsedContent(ctx) {
601
597
  let installation = "";
602
598
  let section = [];
603
599
  let sections = [];
604
- let hasTitle = false;
605
600
  let hasFeatures = false;
606
601
  let indexComplete = false;
607
- let element = dom.window.document.body.firstElementChild;
608
- while (element !== null) {
609
- if (element.matches("h1")) hasTitle = true;
610
- else {
611
- if (element.matches("h2")) {
612
- if (!indexComplete) indexComplete = true;
613
- if (!singlePage && section.length !== 0) {
614
- sections.push(joinLines(section));
615
- section = [];
616
- }
617
- }
618
- let { outerHTML } = element;
619
- if (indexComplete) section.push(outerHTML);
620
- else if (!hasTitle) {
621
- badges.push(outerHTML);
622
- } else if (!hasFeatures) {
623
- if (element.matches("ul")) {
624
- hasFeatures = true;
625
- features.push(outerHTML);
626
- } else {
627
- let installationCode = getInstallationCode(element);
628
- if (installationCode) installation = installationCode;
629
- else if (description.length === 0) description.push(outerHTML);
630
- else intro.push(outerHTML);
631
- }
632
- } else {
633
- let installationCode = getInstallationCode(element);
634
- if (installationCode) installation = installationCode;
635
- else note.push(outerHTML);
602
+ for (let element of dom.window.document.body.children) {
603
+ if (isBadgeContainer(element)) {
604
+ badges = element.outerHTML;
605
+ continue;
606
+ }
607
+ if (element.matches("h1")) {
608
+ title = element.innerHTML;
609
+ continue;
610
+ }
611
+ if (element.matches("h2")) {
612
+ if (!indexComplete) indexComplete = true;
613
+ if (!singlePage && section.length !== 0) {
614
+ sections.push(joinLines(section));
615
+ section = [];
636
616
  }
637
617
  }
638
- element = element.nextElementSibling;
618
+ let { outerHTML } = element;
619
+ if (indexComplete) {
620
+ section.push(outerHTML);
621
+ continue;
622
+ }
623
+ if (hasFeatures) {
624
+ let installationCode2 = getInstallationCode(element);
625
+ if (installationCode2) installation = installationCode2;
626
+ else note.push(outerHTML);
627
+ continue;
628
+ }
629
+ if (element.matches("ul")) {
630
+ hasFeatures = true;
631
+ features.push(outerHTML);
632
+ continue;
633
+ }
634
+ let installationCode = getInstallationCode(element);
635
+ if (installationCode) {
636
+ installation = installationCode;
637
+ continue;
638
+ }
639
+ if (element.matches("p") && description.length === 0) {
640
+ description.push(outerHTML);
641
+ continue;
642
+ }
643
+ intro.push(outerHTML);
639
644
  }
640
645
  if (section.length !== 0) sections.push(joinLines(section));
641
646
  let postprocess = getSectionPostprocess({
@@ -647,10 +652,6 @@ async function getParsedContent(ctx) {
647
652
  description = [];
648
653
  }
649
654
  if (intro.at(-1) === "<hr>") intro.pop();
650
- if (intro.length === 1) {
651
- descriptionNote = intro;
652
- intro = [];
653
- }
654
655
  for (let i = 0; i < intro.length; i++) {
655
656
  if (intro[i].includes("<br>") && intro[i].startsWith("<p>") && intro[i].endsWith("</p>")) {
656
657
  let s = intro[i].slice(3, -4).replace(/<br>(\r?\n)?(\s*)/g, '</span><br>$1$2<span class="li">');
@@ -658,7 +659,8 @@ async function getParsedContent(ctx) {
658
659
  }
659
660
  }
660
661
  return {
661
- badges: postprocessBadges(joinLines(badges)),
662
+ badges,
663
+ // postprocessBadges(joinLines(badges)),
662
664
  title,
663
665
  description: joinLines(description),
664
666
  descriptionNote: joinLines(descriptionNote),
@@ -696,7 +698,7 @@ function getDefaultCodeStyleContent(cssRoot) {
696
698
  `.trim();
697
699
  }
698
700
  function tweakTypography(s = "") {
699
- return s.replace(/\b(for|in|on|at|with|a|an|the)\s+/gi, "$1\xA0").replace(/\b(React)\s+(apps?)\b/gi, "$1\xA0$2");
701
+ return s.replace(/\b(for|in|on|at|to|with|a|an|the|its)\s+/gi, "$1\xA0").replace(/\b(React)\s+(apps?)\b/gi, "$1\xA0$2");
700
702
  }
701
703
  async function setContent(ctx) {
702
704
  let {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t8/docsgen",
3
- "version": "0.2.29",
3
+ "version": "0.2.31",
4
4
  "description": "",
5
5
  "main": "dist/bin.js",
6
6
  "bin": {
@@ -6,8 +6,8 @@ import { getLocation } from "../getLocation";
6
6
  import { buildNav } from "./buildNav";
7
7
  import { getInstallationCode } from "./getInstallationCode";
8
8
  import { getSectionPostprocess } from "./getSectionPostprocess";
9
+ import { isBadgeContainer } from "./isBadgeContainer";
9
10
  import { joinLines } from "./joinLines";
10
- import { postprocessBadges } from "./postprocessBadges";
11
11
  import { preprocessContent } from "./preprocessContent";
12
12
 
13
13
  const md = new Markdown({
@@ -24,7 +24,7 @@ export async function getParsedContent(ctx: Context) {
24
24
 
25
25
  let { nav, linkMap: navLinkMap } = buildNav(ctx, dom);
26
26
 
27
- let badges: string[] = [];
27
+ let badges = "";
28
28
  let title = "";
29
29
  let description: string[] = [];
30
30
  let descriptionNote: string[] = [];
@@ -36,49 +36,64 @@ export async function getParsedContent(ctx: Context) {
36
36
  let section: string[] = [];
37
37
  let sections: string[] = [];
38
38
 
39
- let hasTitle = false;
40
39
  let hasFeatures = false;
41
40
  let indexComplete = false;
42
41
 
43
- let element = dom.window.document.body.firstElementChild;
42
+ for (let element of dom.window.document.body.children) {
43
+ if (isBadgeContainer(element)) {
44
+ badges = element.outerHTML;
45
+ continue;
46
+ }
44
47
 
45
- while (element !== null) {
46
- if (element.matches("h1")) hasTitle = true;
47
- else {
48
- if (element.matches("h2")) {
49
- if (!indexComplete) indexComplete = true;
48
+ if (element.matches("h1")) {
49
+ title = element.innerHTML;
50
+ continue;
51
+ }
50
52
 
51
- if (!singlePage && section.length !== 0) {
52
- sections.push(joinLines(section));
53
- section = [];
54
- }
55
- }
53
+ if (element.matches("h2")) {
54
+ if (!indexComplete) indexComplete = true;
56
55
 
57
- let { outerHTML } = element;
58
-
59
- if (indexComplete) section.push(outerHTML);
60
- else if (!hasTitle) {
61
- badges.push(outerHTML);
62
- } else if (!hasFeatures) {
63
- if (element.matches("ul")) {
64
- hasFeatures = true;
65
- features.push(outerHTML);
66
- } else {
67
- let installationCode = getInstallationCode(element);
68
-
69
- if (installationCode) installation = installationCode;
70
- else if (description.length === 0) description.push(outerHTML);
71
- else intro.push(outerHTML);
72
- }
73
- } else {
74
- let installationCode = getInstallationCode(element);
75
-
76
- if (installationCode) installation = installationCode;
77
- else note.push(outerHTML);
56
+ if (!singlePage && section.length !== 0) {
57
+ sections.push(joinLines(section));
58
+ section = [];
78
59
  }
79
60
  }
80
61
 
81
- element = element.nextElementSibling;
62
+ let { outerHTML } = element;
63
+
64
+ if (indexComplete) {
65
+ section.push(outerHTML);
66
+ continue;
67
+ }
68
+
69
+ if (hasFeatures) {
70
+ let installationCode = getInstallationCode(element);
71
+
72
+ if (installationCode) installation = installationCode;
73
+ else note.push(outerHTML);
74
+
75
+ continue;
76
+ }
77
+
78
+ if (element.matches("ul")) {
79
+ hasFeatures = true;
80
+ features.push(outerHTML);
81
+ continue;
82
+ }
83
+
84
+ let installationCode = getInstallationCode(element);
85
+
86
+ if (installationCode) {
87
+ installation = installationCode;
88
+ continue;
89
+ }
90
+
91
+ if (element.matches("p") && description.length === 0) {
92
+ description.push(outerHTML);
93
+ continue;
94
+ }
95
+
96
+ intro.push(outerHTML);
82
97
  }
83
98
 
84
99
  if (section.length !== 0) sections.push(joinLines(section));
@@ -99,11 +114,6 @@ export async function getParsedContent(ctx: Context) {
99
114
 
100
115
  if (intro.at(-1) === "<hr>") intro.pop();
101
116
 
102
- if (intro.length === 1) {
103
- descriptionNote = intro;
104
- intro = [];
105
- }
106
-
107
117
  for (let i = 0; i < intro.length; i++) {
108
118
  if (
109
119
  intro[i].includes("<br>") &&
@@ -119,7 +129,7 @@ export async function getParsedContent(ctx: Context) {
119
129
  }
120
130
 
121
131
  return {
122
- badges: postprocessBadges(joinLines(badges)),
132
+ badges, // postprocessBadges(joinLines(badges)),
123
133
  title,
124
134
  description: joinLines(description),
125
135
  descriptionNote: joinLines(descriptionNote),
@@ -0,0 +1,18 @@
1
+ let badgeImageSelector =
2
+ 'img[src^="https://img.shields.io/"], img[src^="https://flat.badgen.net/"]';
3
+
4
+ export function isBadgeContainer(element: Element) {
5
+ if (!element.matches("p") || element.children.length === 0) return false;
6
+
7
+ for (let e of element.children) {
8
+ if (
9
+ !e.matches(badgeImageSelector) &&
10
+ (!e.matches("a") ||
11
+ e.children.length !== 1 ||
12
+ !e.children[0]?.matches(badgeImageSelector))
13
+ )
14
+ return false;
15
+ }
16
+
17
+ return true;
18
+ }
@@ -30,7 +30,7 @@ function getDefaultCodeStyleContent(cssRoot: string) {
30
30
 
31
31
  function tweakTypography(s = "") {
32
32
  return s
33
- .replace(/\b(for|in|on|at|with|a|an|the)\s+/gi, "$1\xa0")
33
+ .replace(/\b(for|in|on|at|to|with|a|an|the|its)\s+/gi, "$1\xa0")
34
34
  .replace(/\b(React)\s+(apps?)\b/gi, "$1\xa0$2");
35
35
  }
36
36