aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/helpers')
-rw-r--r--src/utils/helpers/index.ts1
-rw-r--r--src/utils/helpers/pages.tsx8
-rw-r--r--src/utils/helpers/rehype.ts23
-rw-r--r--src/utils/helpers/rss.ts4
4 files changed, 31 insertions, 5 deletions
diff --git a/src/utils/helpers/index.ts b/src/utils/helpers/index.ts
index 79077de..92f9424 100644
--- a/src/utils/helpers/index.ts
+++ b/src/utils/helpers/index.ts
@@ -3,6 +3,7 @@ export * from './images';
export * from './pages';
export * from './reading-time';
export * from './refs';
+export * from './rehype';
export * from './rss';
export * from './schema-org';
export * from './strings';
diff --git a/src/utils/helpers/pages.tsx b/src/utils/helpers/pages.tsx
index 62a582f..7b6bdca 100644
--- a/src/utils/helpers/pages.tsx
+++ b/src/utils/helpers/pages.tsx
@@ -109,9 +109,9 @@ export const getPostsWithUrl = (posts: Article[]): PostData[] =>
* @param {EdgesResponse<RawArticle>[]} rawData - The raw data.
* @returns {PostData[]} An array of posts.
*/
-export const getPostsList = (
+export const getPostsList = async (
rawData: EdgesResponse<RawArticle>[]
-): PostData[] => {
+): Promise<PostData[]> => {
const articlesList: RawArticle[] = [];
rawData.forEach((articleData) => {
articleData.edges.forEach((edge) => {
@@ -120,6 +120,8 @@ export const getPostsList = (
});
return getPostsWithUrl(
- articlesList.map((article) => getArticleFromRawData(article))
+ await Promise.all(
+ articlesList.map(async (article) => getArticleFromRawData(article))
+ )
);
};
diff --git a/src/utils/helpers/rehype.ts b/src/utils/helpers/rehype.ts
new file mode 100644
index 0000000..2716c62
--- /dev/null
+++ b/src/utils/helpers/rehype.ts
@@ -0,0 +1,23 @@
+/**
+ * Update a stringified HTML tree using unified plugins.
+ *
+ * It will parse the provided content to add id to each headings.
+ *
+ * @param {string} content - The page contents.
+ * @returns {string} The updated page contents.
+ */
+export const updateContentTree = async (content: string): Promise<string> => {
+ const { unified } = await import('unified');
+ const rehypeParse = (await import('rehype-parse')).default;
+ const rehypeSanitize = (await import('rehype-sanitize')).default;
+ const rehypeSlug = (await import('rehype-slug')).default;
+ const rehypeStringify = (await import('rehype-stringify')).default;
+
+ return unified()
+ .use(rehypeParse, { fragment: true })
+ .use(rehypeSlug)
+ .use(() => rehypeSanitize({ clobberPrefix: 'h-' }))
+ .use(rehypeStringify)
+ .processSync(content)
+ .toString();
+};
diff --git a/src/utils/helpers/rss.ts b/src/utils/helpers/rss.ts
index 6de60cc..d9c3b1e 100644
--- a/src/utils/helpers/rss.ts
+++ b/src/utils/helpers/rss.ts
@@ -18,8 +18,8 @@ const getAllArticles = async (): Promise<Article[]> => {
const rawArticles = await getArticles({ first: totalArticles });
const articles: Article[] = [];
- rawArticles.edges.forEach((edge) => {
- articles.push(getArticleFromRawData(edge.node));
+ rawArticles.edges.forEach(async (edge) => {
+ articles.push(await getArticleFromRawData(edge.node));
});
return articles;