-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathdevto.ts
64 lines (55 loc) · 2.04 KB
/
devto.ts
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
import axios, { AxiosResponse } from "axios";
import { IArticle } from "../interfaces/interface";
import { convertMarkdownToHtml, sanitizeDevToMarkdown } from "./markdown";
const username = "m_ahmad";
const blogURL = "https://dev.to/m_ahmad";
// Get all users articles from Dev.to and filter by ones with a canonical URL to your blog
export const getAllArticles = async (): Promise<IArticle[]> => {
const params = { username, per_page: 1000 };
const headers = { "api-key": process.env.DEVTO_APIKEY };
const { data }: AxiosResponse = await axios.get(
`https://dev.to/api/articles/me`,
{
params,
headers
}
);
const articles: IArticle[] = data.map(convertDevtoResponseToArticle);
return articles;
};
const blogFilter = (article: IArticle): boolean =>
article.canonical.startsWith(blogURL);
export const getAllBlogArticles = async (): Promise<IArticle[]> => {
const articles = await getAllArticles();
return articles.filter(blogFilter);
};
// Takes a URL and returns the relative slug to your website
export const convertCanonicalURLToRelative = (canonical: string): string => {
return canonical.replace(blogURL, "");
};
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
const convertDevtoResponseToArticle = (data: any): IArticle => {
const slug = convertCanonicalURLToRelative(data.canonical_url);
const markdown = sanitizeDevToMarkdown(data.body_markdown);
const html = convertMarkdownToHtml(markdown);
const article: IArticle = {
id: data.id,
title: data.title,
description: data.description,
publishedAt: data.published_at,
devToSlug: data.slug,
devToPath: data.path,
devToURL: data.url,
commentsCount: data.comments_count,
publicReactionsCount: data.public_reactions_count,
positiveReactionsCount: data.positive_reactions_count,
coverImage: data.cover_image,
tags: data.tag_list,
canonical: data.canonical_url,
collectionId: data.collection_id || -1,
slug,
markdown,
html
};
return article;
};