Skip to content

Commit

Permalink
feat: use local markdown file on dev
Browse files Browse the repository at this point in the history
  • Loading branch information
lukicenturi committed Aug 1, 2023
1 parent a642bfb commit 58a57c5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
12 changes: 6 additions & 6 deletions composables/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { MarkdownParsedContent } from '@nuxt/content/dist/runtime/types';
import type { ComputedRef, Ref } from 'vue';
import { logger } from '~/utils/logger';
import { replacePathPrefix } from '~/utils/api';
import { GITHUB_CONTENT_PREFIX } from '~/utils/constants';
import { CONTENT_PREFIX } from '~/utils/constants';

export interface JobMarkdownContent extends MarkdownParsedContent {
link?: string;
Expand Down Expand Up @@ -40,7 +40,7 @@ export const useMarkdownContent = () => {
let foundJobs;

try {
const remotePath = `${GITHUB_CONTENT_PREFIX}${path}`;
const remotePath = `${CONTENT_PREFIX}${path}`;
// try to fetch from remote
foundJobs = await queryContent<JobMarkdownContent>(remotePath).find();
} catch (e: any) {
Expand All @@ -52,7 +52,7 @@ export const useMarkdownContent = () => {
set(
jobs,
foundJobs?.map((job) => {
job.link = replacePathPrefix(GITHUB_CONTENT_PREFIX, job._path);
job.link = replacePathPrefix(CONTENT_PREFIX, job._path);
return job;
}) ?? [],
);
Expand All @@ -63,12 +63,12 @@ export const useMarkdownContent = () => {
*/
const loadJob = async (path: string): Promise<JobMarkdownContent> => {
try {
const remotePath = `${GITHUB_CONTENT_PREFIX}${path}`;
const remotePath = `${CONTENT_PREFIX}${path}`;
// try to fetch from remote
const data = await queryContent<JobMarkdownContent>(remotePath).findOne();
return {
...data,
link: replacePathPrefix(GITHUB_CONTENT_PREFIX, data?._path),
link: replacePathPrefix(CONTENT_PREFIX, data?._path),
};
} catch (e: any) {
logger.error(e);
Expand All @@ -77,7 +77,7 @@ export const useMarkdownContent = () => {
const data = await queryContent<JobMarkdownContent>(path).findOne();
return {
...data,
link: replacePathPrefix(GITHUB_CONTENT_PREFIX, data?._path),
link: replacePathPrefix(CONTENT_PREFIX, data?._path),
};
}
};
Expand Down
4 changes: 2 additions & 2 deletions layouts/jobs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const { t } = useI18n();

<template>
<Default>
<div class="bg-rui-primary/[.04] py-12 lg:py-20">
<div class="bg-rui-primary/[.04] py-10 lg:py-20">
<div class="container">
<div class="max-w-[768px]">
<div class="mb-3">
<div class="mb-3 text-h6 -ml-1">
<ButtonLink to="/jobs" color="primary" inline>
{{ t('jobs.title') }}
</ButtonLink>
Expand Down
7 changes: 6 additions & 1 deletion nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GITHUB_CONTENT_PREFIX } from './utils/constants';
import { GITHUB_CONTENT_PREFIX, LOCAL_CONTENT_PREFIX } from './utils/constants';

const nonIndexed = [
'/activation',
Expand Down Expand Up @@ -159,6 +159,11 @@ export default defineNuxtConfig({
baseURL: '/md/_content',
},
sources: {
content: {
driver: 'fs',
prefix: LOCAL_CONTENT_PREFIX,
base: 'content',
},
github: {
prefix: GITHUB_CONTENT_PREFIX, // Prefix for routes used to query contents
driver: 'github', // Driver used to fetch contents
Expand Down
8 changes: 4 additions & 4 deletions server/api/_sitemap-urls.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type ParsedContent } from '@nuxt/content/dist/runtime/types';
import { serverQueryContent } from '#content/server';
import { GITHUB_CONTENT_PREFIX } from '~/utils/constants';
import { CONTENT_PREFIX } from '~/utils/constants';

const getLink = (path?: string) =>
path?.startsWith(GITHUB_CONTENT_PREFIX)
? `${path}`.replace(GITHUB_CONTENT_PREFIX, '')
path?.startsWith(CONTENT_PREFIX)
? `${path}`.replace(CONTENT_PREFIX, '')
: path;

export default cachedEventHandler(
Expand All @@ -15,7 +15,7 @@ export default cachedEventHandler(
// try to fetch from remote
jobs = await serverQueryContent(event)
.where({
_path: { $contains: `${GITHUB_CONTENT_PREFIX}/${dir}` },
_path: { $contains: `${CONTENT_PREFIX}/${dir}` },
open: { $eq: true },
})
.only('_path')
Expand Down
7 changes: 7 additions & 0 deletions utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export const LOCAL_CONTENT_PREFIX = '/local';

export const GITHUB_CONTENT_PREFIX = '/remote';

export const CONTENT_PREFIX =
process.env.NODE_ENV === 'development'
? LOCAL_CONTENT_PREFIX
: GITHUB_CONTENT_PREFIX;

0 comments on commit 58a57c5

Please sign in to comment.