astro-accelerator 0.0.94 → 0.0.96

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/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.94",
2
+ "version": "0.0.96",
3
3
  "author": "Steve Fenton",
4
4
  "name": "astro-accelerator",
5
5
  "description": "A super-lightweight, accessible, SEO-friendly starter project for Astro",
@@ -28,6 +28,8 @@
28
28
  "astro": "^2.5.7",
29
29
  "astro-accelerator-utils": "^0.2.28",
30
30
  "hast-util-from-selector": "^2.0.1",
31
+ "html-to-text": "^9.0.5",
32
+ "keyword-extractor": "^0.0.25",
31
33
  "remark-directive": "^2.0.1",
32
34
  "sharp": "^0.32.1"
33
35
  },
@@ -916,8 +916,8 @@ pre.astro-code code {
916
916
  float: right;
917
917
  overflow: hidden;
918
918
  display: block;
919
- width: 1.2em;
920
- height: 1.5em;
919
+ width: 1.3em;
920
+ height: 1.6em;
921
921
  opacity: 0.5;
922
922
  }
923
923
 
@@ -57,47 +57,37 @@ function search(s) {
57
57
  item.score = 0;
58
58
  item.matchedHeadings = [];
59
59
 
60
- // Imagine the user searched for "Kitchen Sink"
61
- // The scores are arranged below from highest to lowest relevance
60
+ // The user searches for "Kitchen Sink"
62
61
 
63
- // If the title contains a whole word match
64
- queryTerms.forEach(t => {
65
- if (containsWord(item.safeTitle, t)) {
66
- item.score = item.score + 120;
67
- }
68
- });
62
+ // Part 1 - Phrase Matches, i.e. "Kitchen Sink"
69
63
 
70
- // If the title contains "Kitchen Sink"
64
+ // Title
71
65
  if (contains(item.safeTitle, currentQuery)) {
72
66
  item.score = item.score + 60;
73
67
  }
74
68
 
75
- // If a heading contains "Kitchen Sink"
69
+ // Headings
76
70
  item.headings.forEach(c => {
77
- queryTerms.forEach(t => {
78
- if (containsWord(c.safeText, t)) {
79
- item.score = item.score + 40;
80
- }
81
- });
82
-
83
71
  if (contains(c.safeText, currentQuery)) {
84
72
  item.score = item.score + 20;
85
73
  item.matchedHeadings.push(c);
86
74
  }
87
75
  });
88
76
 
89
- // If the title contains "Kitchen Sink"
77
+ // Description
90
78
  if (contains(item.description, currentQuery)) {
91
79
  item.score = item.score + 20;
92
80
  }
81
+
82
+ // Part 2 - Term Matches, i.e. "Kitchen" or "Sink"
93
83
 
94
84
  queryTerms.forEach(term => {
95
- // If the title contains "Kitchen" or "Sink"
85
+ // Title
96
86
  if (contains(item.safeTitle, term)) {
97
87
  item.score = item.score + 40;
98
88
  }
99
89
 
100
- // If a heading contains "Kitchen" or "Sink"
90
+ // Headings
101
91
  item.headings.forEach(c => {
102
92
  if (contains(c.safeText, term)) {
103
93
  item.score = item.score + 15;
@@ -107,17 +97,22 @@ function search(s) {
107
97
  }
108
98
  });
109
99
 
110
- // If the description contains "Kitchen" or "Sink"
100
+ // Description
111
101
  if (contains(item.description, term)) {
112
- item.score = item.score + 10;
102
+ item.score = item.score + 15;
113
103
  }
114
104
 
115
- // If a tag contains "Kitchen" or "Sink"
105
+ // Tags
116
106
  item.tags.forEach(t => {
117
107
  if (contains(t, term)) {
118
- item.score = item.score + 5;
108
+ item.score = item.score + 15;
119
109
  }
120
110
  });
111
+
112
+ // Keywords
113
+ if (contains(item.keywords, term)) {
114
+ item.score = item.score + 15;
115
+ }
121
116
  })
122
117
 
123
118
  if (item.score > 0) {
package/src/env.d.ts ADDED
@@ -0,0 +1 @@
1
+ /// <reference types="astro/client" />
@@ -3,6 +3,9 @@
3
3
  import { Accelerator, PostFiltering } from 'astro-accelerator-utils';
4
4
  import type { MarkdownInstance } from 'astro';
5
5
  import { SITE } from '@config';
6
+ import { htmlToText } from 'html-to-text';
7
+ import keywordExtractor from 'keyword-extractor';
8
+
6
9
 
7
10
  const getData = async () => {
8
11
  //@ts-ignore
@@ -26,13 +29,33 @@ const getData = async () => {
26
29
 
27
30
  const headings = await page.getHeadings();
28
31
  const title = await accelerator.markdown.getTextFrom(page.frontmatter.title ?? '');
29
-
32
+ const content = page.compiledContent ? page.compiledContent() : '';
33
+ let counted: { word: string, count: number }[] = [];
34
+
35
+ if (content) {
36
+ const text = htmlToText(content, { wordwrap: false });
37
+
38
+ const words = keywordExtractor.extract(text, {
39
+ language: 'english',
40
+ return_changed_case: true
41
+ });
42
+
43
+ function unique (value: string, index: number, array: string[]) {
44
+ return array.indexOf(value) === index;
45
+ }
46
+ const uniques = words.filter(unique);
47
+ counted = uniques.map((w) => {
48
+ return { word: w, count: words.filter(wd => wd === w).length };
49
+ }).filter(e => e.word.replace(/[^a-z]+/g, '').length > 1 && e.count > 1);
50
+ }
51
+
30
52
  items.push({
31
53
  title: title,
32
54
  headings: headings.map(h => {
33
55
  return {text: h.text, slug: h.slug }
34
56
  }),
35
57
  description: page.frontmatter.description ?? '',
58
+ keywords: counted.map(c => c.word).join(' '),
36
59
  tags: page.frontmatter.tags ?? [],
37
60
  url: SITE.url + accelerator.urlFormatter.formatAddress(url),
38
61
  date: page.frontmatter.pubDate ?? ''