astro-accelerator-utils 0.0.31 → 0.0.33

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/index.d.mts CHANGED
@@ -1,7 +1,9 @@
1
1
  import * as PostQueries from "./lib/postQueries.mjs";
2
2
  import * as PostFiltering from "./lib/postFiltering.mjs";
3
3
  import * as PostOrdering from "./lib/postOrdering.mjs";
4
+ import * as PostPaging from "./lib/postPaging.mjs";
4
5
  import * as Cache from "./lib/cache.mjs";
5
6
  import * as Dates from "./lib/dates.mjs";
7
+ import * as Markdown from "./lib/markdown.mjs";
6
8
  import * as Urls from "./lib/urls.mjs";
7
- export { PostQueries, PostFiltering, PostOrdering, Cache, Dates, Urls };
9
+ export { PostQueries, PostFiltering, PostOrdering, PostPaging, Cache, Dates, Markdown, Urls };
package/index.mjs CHANGED
@@ -1,15 +1,19 @@
1
- import * as PostQueries from "./lib/postQueries.mjs";
2
- import * as PostFiltering from "./lib/postFiltering.mjs";
3
- import * as PostOrdering from "./lib/postOrdering.mjs";
4
- import * as Cache from "./lib/cache.mjs";
5
- import * as Dates from "./lib/dates.mjs";
6
- import * as Urls from "./lib/urls.mjs";
1
+ import * as PostQueries from './lib/postQueries.mjs';
2
+ import * as PostFiltering from './lib/postFiltering.mjs';
3
+ import * as PostOrdering from './lib/postOrdering.mjs';
4
+ import * as PostPaging from './lib/postPaging.mjs';
5
+ import * as Cache from './lib/cache.mjs';
6
+ import * as Dates from './lib/dates.mjs';
7
+ import * as Markdown from './lib/markdown.mjs';
8
+ import * as Urls from './lib/urls.mjs';
7
9
 
8
10
  export {
9
11
  PostQueries,
10
12
  PostFiltering,
11
13
  PostOrdering,
14
+ PostPaging,
12
15
  Cache,
13
16
  Dates,
17
+ Markdown,
14
18
  Urls
15
19
  };
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Converts markdown to HTML without wrapping in a <p>
3
+ * @param {string} markdown
4
+ * @returns {Promise<string>}
5
+ */
6
+ export function getInlineHtmlFrom(markdown: string): Promise<string>;
7
+ /**
8
+ * Converts markdown to HTML
9
+ * @param {string} markdown
10
+ * @returns {Promise<string>}
11
+ */
12
+ export function getHtmlFrom(markdown: string): Promise<string>;
13
+ /**
14
+ * Converts markdown to plain text
15
+ * @param {string} markdown
16
+ * @returns {Promise<string>}
17
+ */
18
+ export function getTextFrom(markdown: string): Promise<string>;
@@ -0,0 +1,45 @@
1
+ import { unified } from 'unified';
2
+ import remarkParse from 'remark-parse';
3
+ import remarkRehype from 'remark-rehype'
4
+ import rehypeStringify from 'rehype-stringify';
5
+
6
+ /**
7
+ * Converts markdown to HTML without wrapping in a <p>
8
+ * @param {string} markdown
9
+ * @returns {Promise<string>}
10
+ */
11
+ export async function getInlineHtmlFrom(markdown) {
12
+ let html = await getHtmlFrom(markdown);
13
+
14
+ // There may be a better way to unwrap this... maybe a visitor
15
+ if (html.substring(0, 3) == '<p>' && html.substring(html.length - 4) == '</p>') {
16
+ html = html.substring(3, html.length - 4);
17
+ }
18
+
19
+ return html;
20
+ }
21
+
22
+ /**
23
+ * Converts markdown to HTML
24
+ * @param {string} markdown
25
+ * @returns {Promise<string>}
26
+ */
27
+ export async function getHtmlFrom(markdown) {
28
+ const vfile = await unified()
29
+ .use(remarkParse)
30
+ .use(remarkRehype)
31
+ .use(rehypeStringify)
32
+ .process(markdown)
33
+
34
+ return String(vfile);
35
+ }
36
+
37
+ /**
38
+ * Converts markdown to plain text
39
+ * @param {string} markdown
40
+ * @returns {Promise<string>}
41
+ */
42
+ export async function getTextFrom(markdown) {
43
+ const html = await getInlineHtmlFrom(markdown);
44
+ return html.replace(/<.*?>/g, '');
45
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @typedef { import("../types/Link").Link } Link
3
+ */
4
+ /**
5
+ * Provides a list of paging links, 1 ... 3 4 5 ... 7
6
+ * @param {number} limit
7
+ * @param {number} numberOfPages
8
+ * @param {number} currentPage
9
+ * @param {string} url
10
+ * @returns {Link[]}
11
+ */
12
+ export function getPageLinks(limit: number, numberOfPages: number, currentPage: number, url: string): Link[];
13
+ export type Link = import("../types/Link").Link;
@@ -0,0 +1,77 @@
1
+ /**
2
+ * @typedef { import("../types/Link").Link } Link
3
+ */
4
+
5
+ /**
6
+ * Provides a list of paging links, 1 ... 3 4 5 ... 7
7
+ * @param {number} limit
8
+ * @param {number} numberOfPages
9
+ * @param {number} currentPage
10
+ * @param {string} url
11
+ * @returns {Link[]}
12
+ */
13
+ export function getPageLinks(limit, numberOfPages, currentPage, url) {
14
+ /** @type {Link[]} */
15
+ const pageLinks = [];
16
+
17
+ let start = 0;
18
+ let end = numberOfPages;
19
+
20
+ if (numberOfPages > limit) {
21
+ start = (currentPage - (limit - 1) / 2) - 1;
22
+ if (start < 0) {
23
+ start = 0;
24
+ }
25
+
26
+ end = (start + limit);
27
+ if (end > numberOfPages) {
28
+ end = numberOfPages;
29
+ start = numberOfPages - limit;
30
+ }
31
+ }
32
+
33
+ if (start == 1) {
34
+ pageLinks.push({
35
+ title: '1',
36
+ url: url.replace('/' + currentPage, '/' + 1),
37
+ ariaCurrent: false,
38
+ class: ''
39
+ });
40
+ } else if (start > 1) {
41
+ pageLinks.push({
42
+ title: '1',
43
+ url: url.replace('/' + currentPage, '/' + 1),
44
+ ariaCurrent: false,
45
+ class: 'paging-collapse-after'
46
+ });
47
+ }
48
+
49
+ for (let i = start; i < end; i++) {
50
+ const userPage = i + 1;
51
+ pageLinks.push({
52
+ title: userPage.toString(),
53
+ url: url.replace('/' + currentPage, '/' + userPage),
54
+ ariaCurrent: userPage == currentPage ? 'page' : false,
55
+ class: ''
56
+ });
57
+ }
58
+
59
+ if (end < (numberOfPages - 1)) {
60
+ pageLinks.push({
61
+ title: numberOfPages.toString(),
62
+ url: url.replace('/' + currentPage, '/' + numberOfPages),
63
+ ariaCurrent: false,
64
+ class: 'paging-collapse-before'
65
+ });
66
+ } else if (end < numberOfPages) {
67
+ pageLinks.push({
68
+ title: numberOfPages.toString(),
69
+ url: url.replace('/' + currentPage, '/' + numberOfPages),
70
+ ariaCurrent: false,
71
+ class: ''
72
+ });
73
+ }
74
+
75
+ return pageLinks;
76
+ }
77
+
@@ -27,9 +27,9 @@ export function getAuthorInfo(slug: string): Promise<AuthorInfo>;
27
27
  * Returns a list of breadcrumbs
28
28
  * @param {URL} currentUrl
29
29
  * @param {Site} site
30
- * @returns {NavPage[]}
30
+ * @returns {Promise<NavPage[]>}
31
31
  */
32
- export function getBreadcrumbs(currentUrl: URL, site: Site): NavPage[];
32
+ export function getBreadcrumbs(currentUrl: URL, site: Site): Promise<NavPage[]>;
33
33
  /**
34
34
  * Converts a MarkdownInstance into a NavPage
35
35
  * @param {MarkdownInstance} page
@@ -144,7 +144,7 @@ export async function getAuthorInfo (slug) {
144
144
  * Returns a list of breadcrumbs
145
145
  * @param {URL} currentUrl
146
146
  * @param {Site} site
147
- * @returns {NavPage[]}
147
+ * @returns {Promise<NavPage[]>}
148
148
  */
149
149
  export async function getBreadcrumbs (currentUrl, site) {
150
150
  const allPages = await getPages();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-accelerator-utils",
3
- "version": "0.0.31",
3
+ "version": "0.0.33",
4
4
  "description": "Astro utilities for Astro Accelerator.",
5
5
  "main": "index.mjs",
6
6
  "files": [
@@ -30,6 +30,10 @@
30
30
  "homepage": "https://astro.stevefenton.co.uk/",
31
31
  "devDependencies": {
32
32
  "@types/node": "^18.11.9",
33
- "typescript": "^4.9.3"
33
+ "typescript": "^4.9.3",
34
+ "unified": "^10.1.2",
35
+ "remark-parse": "^10.0.1",
36
+ "remark-rehype": "^10.1.0",
37
+ "rehype-stringify": "^9.0.3"
34
38
  }
35
39
  }
@@ -0,0 +1,6 @@
1
+ export interface Link {
2
+ title: string;
3
+ url: string;
4
+ ariaCurrent: 'page' | false;
5
+ class: string;
6
+ }