Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

blog to api (#370)

Maksim Mrug9987f384646d
diff --git a/.github/workflows/fly.yml b/.github/workflows/fly.yml
index 9684db3de..14caa2926 100644
--- a/.github/workflows/fly.yml
+++ b/.github/workflows/fly.yml
@@ -11,9 +11,6 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: superfly/flyctl-actions/setup-flyctl@master
- - name: Pull submodules
- run: git submodule foreach git pull
- - name: Deploy to fly
run: flyctl deploy --remote-only
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
diff --git a/.gitmodules b/.gitmodules
deleted file mode 100644
index 73e1f8bf6..000000000
--- a/.gitmodules
+++ /dev/null
@@ -1,3 +0,0 @@
-[submodule "blog-posts"]
- path = blog-posts
- url = https://github.com/Swetrix/blog-posts.git
diff --git a/app/api/index.ts b/app/api/index.ts
index 52e54aea6..65d167530 100644
--- a/app/api/index.ts
+++ b/app/api/index.ts
@@ -1144,3 +1144,39 @@ export const changeSubscriptionPlan = (planId: number) =>
debug('%s', error)
throw error
})
+
+export const getBlogPosts = () =>
+ api
+ .get('v1/blog')
+ .then((response) => response.data)
+ .catch((error) => {
+ debug('%s', error)
+ throw error
+ })
+
+export const getBlogPost = (slug: string) =>
+ api
+ .get(`v1/blog/${slug}`)
+ .then((response) => response.data)
+ .catch((error) => {
+ debug('%s', error)
+ throw error
+ })
+
+export const getSitemap = () =>
+ api
+ .get('blog/sitemap')
+ .then((response) => response.data)
+ .catch((error) => {
+ debug('%s', error)
+ throw error
+ })
+
+export const getBlogPostWithCategory = (category: string, slug: string) =>
+ api
+ .get(`v1/blog/${category}/${slug}`)
+ .then((response) => response.data)
+ .catch((error) => {
+ debug('%s', error)
+ throw error
+ })
diff --git a/app/utils/getPosts.ts b/app/utils/getPosts.ts
index 783a1ac05..d5367c5f8 100644
--- a/app/utils/getPosts.ts
+++ b/app/utils/getPosts.ts
@@ -1,16 +1,8 @@
-import path from 'path'
-import fs from 'fs/promises'
-import parseFrontMatter from 'front-matter'
import { marked } from 'marked'
-import _map from 'lodash/map'
-import _find from 'lodash/find'
-import _includes from 'lodash/includes'
import _filter from 'lodash/filter'
-import _endsWith from 'lodash/endsWith'
-import _replace from 'lodash/replace'
import _isEmpty from 'lodash/isEmpty'
-import _size from 'lodash/size'
import { LoaderFunction } from '@remix-run/node'
+import { getBlogPosts, getBlogPost, getBlogPostWithCategory, getSitemap } from 'api'
const renderer = new marked.Renderer()
@@ -33,110 +25,73 @@ export type PostMarkdownAttributes = {
hidden?: boolean
}
-interface IParseFontMatter {
- attributes: PostMarkdownAttributes
- body: string
-}
-
-export const postsPath = path.join(__dirname, '..', 'blog-posts', 'posts')
-
// Removes first 10 characters from the string (i.e. 2023-10-07-)
export const getSlugFromFilename = (filename: string) => filename.substring(11)
export const getDateFromFilename = (filename: string) => filename.substring(0, 10)
-const findFilenameBySlug = (list: string[], handle: string) => {
- return _find(list, (item) => _includes(item, handle))
-}
-
export const getSitemapFileNames = async (category?: string, infiniteRecursive?: boolean): Promise<any[]> => {
- const allFiles = await fs.readdir(category ? path.join(postsPath, category) : postsPath) as string[]
- const directories = _filter(allFiles, (file) => !_endsWith(file, '.md'))
+ const sitemap = await getSitemap()
- if (_isEmpty(directories)) {
- return allFiles
+ if (infiniteRecursive) {
+ return sitemap
}
- const files = _filter(allFiles, (file) => _endsWith(file, '.md'))
-
- let filesInDirectories: any[] = []
-
- for (let i = 0; i < _size(directories); ++i) {
- const directory = directories[i]
-
- // set to true for infinite depth directory look-up
- const _files = await getSitemapFileNames(directory, infiniteRecursive)
- filesInDirectories = [
- ...filesInDirectories,
- ..._map(_files, (file) => [directory, file]),
- ]
+ if (category) {
+ return _filter(sitemap, (item) => item === category)
}
- return [
- ...files,
- ...filesInDirectories,
- ]
-}
+ return sitemap
+}
-export const getFileNames = async (category?: string): Promise<string[]> => {
- let files
+export async function getPost(slug: string, category?: string) {
+ let post: any = null
if (category) {
- files = await fs.readdir(path.join(postsPath, category)) as string[]
+ await getBlogPostWithCategory(category, slug)
+ .then((data) => {
+ post = data
+ })
+ .catch((error) => {
+ console.error(error)
+ })
} else {
- files = await fs.readdir(postsPath) as string[]
+ await getBlogPost(slug)
+ .then((data) => {
+ post = data
+ })
+ .catch((error) => {
+ console.error(error)
+ })
}
- return _filter(files, (file) => _endsWith(file, '.md'))
-}
-
-export async function getPost(slug: string, category?: string) {
- const files = await getFileNames(category)
- const filename = findFilenameBySlug(files, slug)
-
- if (!filename) {
+ if (!post || _isEmpty(post)) {
return null
}
- const filepath = category
- ? path.join(postsPath, category, filename)
- : path.join(postsPath, filename)
-
- const file = await fs.readFile(filepath)
- const { attributes, body }: IParseFontMatter = parseFrontMatter(file.toString())
-
return {
slug,
- title: attributes.title,
- html: marked(body, { renderer }),
- hidden: attributes.hidden,
- intro: attributes.intro,
- date: attributes.date,
- author: attributes.author,
- nickname: attributes.nickname,
+ title: post.attributes?.title,
+ html: marked(post.body, { renderer }),
+ hidden: post.attributes?.hidden,
+ intro: post.attributes?.intro,
+ date: post.attributes?.date,
+ author: post.attributes?.author,
+ nickname: post.attributes?.nickname,
}
}
export const blogLoader: LoaderFunction = async () => {
- const dir = await fs.readdir(postsPath)
- const filtered = _filter(dir, (file) => _endsWith(file, '.md'))
-
- return Promise.all(
- _map(filtered, async filename => {
- const file = await fs.readFile(
- path.join(postsPath, filename)
- )
- const { attributes }: IParseFontMatter = parseFrontMatter(
- file.toString()
- )
- return {
- slug: _replace(getSlugFromFilename(filename), /\.md$/, ''),
- title: attributes.title,
- hidden: attributes.hidden,
- intro: attributes.intro,
- date: attributes.date,
- author: attributes.author,
- nickname: attributes.nickname,
- }
+ const data = await getBlogPosts()
+ .then((data) => {
+ return data
})
- )
+ .catch((error) => {
+ console.error(error)
+ })
+
+ if (!data || _isEmpty(data)) {
+ return null
+ }
+
+ return data
}
diff --git a/blog-posts b/blog-posts
deleted file mode 160000
index 24e9a99bc..000000000
--- a/blog-posts
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit 24e9a99bca44d3193c81998abfbd62e7410126fa
diff --git a/package.json b/package.json
index aa67d7b35..4a2d1a84a 100644
--- a/package.json
+++ b/package.json
@@ -5,7 +5,6 @@
"build": "remix build",
"dev": "remix dev",
"deploy": "flyctl deploy --remote-only",
- "refresh-submodules": "git submodule foreach git pull",
"import-env": "node fly_import_env.js --env-file .env",
"dev:staging": "cross-env STAGING=true remix dev",
"start": "remix-serve build",
@@ -43,7 +42,6 @@
"dom-to-image": "^2.6.0",
"file-saver": "^2.0.5",
"flatpickr": "^4.6.13",
- "front-matter": "^4.0.2",
"i18n-iso-countries": "^7.6.0",
"i18next": "^23.4.9",
"i18next-browser-languagedetector": "^7.1.0",