Skip to content

Commit

Permalink
Merge pull request #51 from rob-at-airwalk/main
Browse files Browse the repository at this point in the history
Feat: Print mode
  • Loading branch information
rob-at-airwalk committed Jul 8, 2024
2 parents dd13912 + 8a27ab2 commit 16367c0
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 14 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"react-dom": "^18.3.1",
"react-hook-form": "^7.51.2",
"react-syntax-highlighter": "^15.5.0",
"recma-mdx-escape-missing-components": "^1.1.0",
"rehype-slug": "^6.0.0",
"remark-frontmatter": "^5.0.0",
"remark-gfm": "^4.0.0",
Expand Down
9 changes: 7 additions & 2 deletions src/_components/Layouts/ContentPrint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ export default function ContentPrint({ children }: PagedOutputProps) {
const currentPath = usePathname();

function handleClose() {
const newPath = currentPath.replace(/\/print$/, '');
router.replace(newPath);
const pathnameArray = currentPath.split('/');

// Replace 'print' with 'view' in the URL path
pathnameArray[2] = 'view';

const newPathname = pathnameArray.join('/');
router.push(newPathname);
}
function handlePrint() {
window.print();
Expand Down
22 changes: 18 additions & 4 deletions src/_components/Layouts/ContentViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,24 @@ export function ContentViewer({
return <ContentSkeleton topBarHeight={topBarHeight} />;
}
if (pageContent) {
const { mdxContent: Page, frontmatter } = loadMDX(
pageContent,
context?.file?.endsWith('.md') ? 'md' : 'mdx'
);
let frontmatter;
let mdxContent;
try {
({ mdxContent, frontmatter } = loadMDX(
pageContent,
context?.file?.endsWith('.md') ? 'md' : 'mdx'
));
} catch (error: any) {
try {
({ mdxContent, frontmatter } = loadMDX(
`---\ntitle: Error\n---\nError converting MDX\n${error.message}`,
'md'
));
} catch (err: any) {
logger.error({ msg: 'Error converting MDX', err });
}
}
const Page = mdxContent;
logger.info({ msg: 'ContentViewer', frontmatter });
return (
<AsideAndMainContainer>
Expand Down
4 changes: 2 additions & 2 deletions src/_components/Layouts/MenuWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ export default function MenuWrapper({
maxWidth: '100% !important',
pt: '2%',
ml: 0,
pl: '0 !important',
pr: '0 !important',
// pl: '0 !important',
// pr: '0 !important',
}}
>
{children && children}
Expand Down
3 changes: 3 additions & 0 deletions src/_components/Layouts/lib/loadMDX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import withTocExport from '@stefanprobst/rehype-extract-toc/mdx';
import matter from 'gray-matter';
import type { MDXContent } from 'mdx/types';
import * as runtime from 'react/jsx-runtime';
import recmaMdxEscapeMissingComponents from 'recma-mdx-escape-missing-components';
import withSlugs from 'rehype-slug';
import remarkFrontmatter from 'remark-frontmatter';
import remarkGfm from 'remark-gfm';
Expand Down Expand Up @@ -44,6 +45,7 @@ export function loadMDX(
format: format as 'detect' | 'md' | 'mdx' | null | undefined,
remarkPlugins,
rehypePlugins,
recmaPlugins: [recmaMdxEscapeMissingComponents],
});
const frontmatter = data || {};
if (tableOfContents) {
Expand All @@ -60,6 +62,7 @@ export function loadMDX(
format: format as 'detect' | 'md' | 'mdx' | null | undefined,
remarkPlugins,
rehypePlugins,
recmaPlugins: [recmaMdxEscapeMissingComponents],
});
const frontmatter = data || {};
if (tableOfContents) {
Expand Down
151 changes: 151 additions & 0 deletions src/app/docs/[mode]/[branch]/[[...path]]/@print/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import type { Metadata } from 'next';
import matter from 'gray-matter';

import React from 'react';
import { ContentLoader, ContentPrint } from '@/components/Layouts';

import { siteConfig } from '@/config';
import { notFound } from 'next/navigation';
import { getFileContent } from '@/lib/Github';
import { getLogger } from '@/lib/Logger';
import type { ContentItem, MatterData } from '@/lib/Types';
const logger = getLogger().child({ namespace: 'docs/page' });
logger.level = 'error';
export const metadata: Metadata = {
title: 'Airview',
description: 'Airview AI',
};

async function checkFrontmatter(content: string, context: ContentItem) {
const matterData = matter(content, {
excerpt: false,
}).data as MatterData;
if (matterData) {
Object.keys(matterData).forEach((key) => {
if (matterData && matterData[key] && matterData[key] instanceof Date) {
matterData[key] = (matterData[key] as Date).toISOString();
}
});
}
if (
matterData &&
matterData.external_repo &&
matterData.external_owner &&
matterData.external_path &&
matterData.git_provider
) {
const { external_repo, external_owner, external_path } = matterData;
const owner = external_owner as string;
const repo = external_repo as string;
const branch = context.branch;
const file = external_path as string;
let pageContent;
try {
pageContent = await getFileContent({ owner, repo, path: file });
} catch (error) {
logger.error({ msg: 'checkFrontmatter: ', error });
}
let linkedContent = '';
if (pageContent && pageContent.content) {
linkedContent = pageContent?.content
? Buffer.from(pageContent.content).toString()
: '';
}
return {
content: linkedContent,
context: { ...context, linked: { owner, repo, branch, path: file } },
};
} else {
return { content, context };
}
}

export default async function Page({
params,
}: {
params: { mode: 'view' | 'edit' | 'print'; branch: string; path: string[] };
}) {
if (
params.path &&
params.path[0] &&
siteConfig.content[params.path[0] as keyof typeof siteConfig.content]
) {
let path = params.path;

let file = '';

// if 'related_config' is somewhere in the path, then the file is the last element in the path
if (path.includes('related_content')) {
// join all the parts after related_config
file = path
.slice(path.indexOf('related_content') + 1)
.join('/') as string;
} else {
file = path.join('/') as string;
}

// const file = path.join("/") as string;
let pageContent;
let pageContentText;
let loading = false;
const contentKey = params.path[0] as keyof typeof siteConfig.content;
const branch = () =>
params.branch === 'default'
? siteConfig?.content?.[contentKey]?.branch
: decodeURIComponent(params.branch);
const contentConfig = {
...siteConfig?.content?.[contentKey],
file: file,
branch: branch(),
} as ContentItem;

// content page
if (file.endsWith('.md') || file.endsWith('.mdx')) {
// const contentKey = params.path[0] as keyof typeof siteConfig.content;
// const contentConfig = siteConfig?.content?.[contentKey];
const isGithubRepo =
contentConfig.owner &&
contentConfig.repo &&
contentConfig.branch &&
file;

if (isGithubRepo) {
const { owner, repo, branch } = contentConfig;
pageContent = await getFileContent({ owner, repo, path: file, branch });
if (pageContent && pageContent.content) {
pageContentText = pageContent?.content
? Buffer.from(pageContent.content).toString()
: '';
} else {
return notFound();
}

const { content: linkedPageContentText, context } =
await checkFrontmatter(pageContentText || '', contentConfig); // check for frontmatter context
pageContentText = linkedPageContentText;

if (pageContent && pageContent.content && pageContentText) {
return (
<main>
<ContentPrint>
<ContentLoader
pageContent={pageContentText}
context={context}
loading={loading}
/>
</ContentPrint>
</main>
);
} else {
return notFound();
}
} else {
return notFound();
}
} else {
notFound();
}
} else {
return notFound();
}
}
11 changes: 5 additions & 6 deletions src/app/docs/[mode]/[branch]/[[...path]]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { notFound } from 'next/navigation';
import { Metadata } from 'next';
import { siteConfig } from '@/config';
import { getLogger } from '@/lib/Logger';
Expand All @@ -14,13 +13,13 @@ export const metadata: Metadata = {
export default function Layout({
view,
edit,
// print,
print,
index,
params,
}: {
view: React.ReactNode;
edit: React.ReactNode;
// print: React.ReactNode
print: React.ReactNode;
index: React.ReactNode;
// mode = 'view' | 'edit' | 'print'
// branch: string
Expand All @@ -38,8 +37,8 @@ export default function Layout({
return view;
case 'edit':
return edit;
// case 'print':
// return print;
case 'print':
return print;
}
return notFound();
// return notFound();
}

0 comments on commit 16367c0

Please sign in to comment.