Skip to content

Commit

Permalink
Merge pull request #54 from mrjvs/dev
Browse files Browse the repository at this point in the history
Guider v1.0.2
  • Loading branch information
mrjvs authored Apr 25, 2024
2 parents eab76e5 + db796e1 commit fac8707
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 75 deletions.
2 changes: 1 addition & 1 deletion packages/guider/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@neato/guider",
"version": "1.0.1",
"version": "1.0.2",
"description": "Beautiful documentation sites, without all the hassle",
"main": "./dist/index.js",
"type": "module",
Expand Down
7 changes: 6 additions & 1 deletion packages/guider/src/client/partials/header/search/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type ContentDocument = {
id: number;
url: string;
title: string;
pageTitle?: string;
content: string;
},
never[]
Expand All @@ -21,13 +22,15 @@ type SearchData = Record<
heading?: { id: string; depth: number; text: string };
content: string;
}[];
pageTitle: string;
}
>;

export type SearchResult = {
id: string;
type: 'section';
title: string;
pageTitle?: string;
content: string;
url: string;
};
Expand All @@ -53,7 +56,7 @@ async function fetchDocument(basePath: string, key: string) {
document: {
id: 'id',
index: ['title', 'content'],
store: ['content', 'url', 'title'],
store: ['content', 'url', 'title', 'pageTitle'],
},
context: {
resolution: 9,
Expand All @@ -69,6 +72,7 @@ async function fetchDocument(basePath: string, key: string) {
searchDocument.add({
id: pageId,
title: section.heading?.text ?? '',
pageTitle: data.pageTitle,
url: url + (section.heading ? `#${section.heading.id}` : ''),
content: section.content,
});
Expand Down Expand Up @@ -113,6 +117,7 @@ export function useSearch(key: string) {
type: 'section',
id: res.id.toString(),
title: res.doc.title,
pageTitle: res.doc.pageTitle,
content: res.doc.content,
url: res.doc.url,
};
Expand Down
12 changes: 9 additions & 3 deletions packages/guider/src/client/partials/header/search/screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ function SearchMessage(props: { title: string; text: string; icon: string }) {
);
}

function SearchResults(props: { results: SearchResult[] }) {
function SearchResults(props: {
results: SearchResult[];
onClose?: () => void;
}) {
return (
<div className="gd-p-2 gd-space-y-2 gd-border-t gd-border-bgLightest gd-max-h-[22rem] gd-overflow-y-auto">
{props.results.map((v) => {
let title = v.pageTitle ? `${v.pageTitle} / ${v.title}` : v.title;
if (v.pageTitle === v.title) title = v.title;
return (
<Combobox.Option key={v.id} value={v} as="article">
{({ active }) => (
<Link
href={v.url}
key={v.id}
onClick={props.onClose}
className={classNames(
'gd-p-3 hover:gd-bg-primaryDark gd-group gd-duration-100 gd-transition-colors gd-rounded-lg gd-flex gd-gap-4 gd-items-center gd-relative',
{
Expand All @@ -55,7 +61,7 @@ function SearchResults(props: { results: SearchResult[] }) {
</div>
<div className="gd-pr-4">
<h2 className="gd-text-white gd-line-clamp-1 gd-text-sm gd-font-bold">
{v.title}
{title}
</h2>
<p
className={classNames(
Expand Down Expand Up @@ -145,7 +151,7 @@ export function SearchScreen(props: {
text="Try some different keywords"
/>
) : query.length > 0 && results ? (
<SearchResults results={results} />
<SearchResults results={results} onClose={props.onClose} />
) : null}
</Combobox.Options>
</div>
Expand Down
78 changes: 8 additions & 70 deletions packages/guider/src/webpack/loader/md-loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { compile } from '@mdx-js/mdx';
import remarkFrontmatter from 'remark-frontmatter';
import type { Heading } from '@vcarl/remark-headings';
import remarkHeadings from '@vcarl/remark-headings';
import remarkHeadingId from 'remark-heading-id';
import grayMatter from 'gray-matter';
Expand All @@ -8,25 +9,17 @@ import rehypeExtractExcerpt from 'rehype-extract-excerpt';
import remarkLinkRewrite from 'remark-link-rewrite';
import { remarkNpm2Yarn } from '@theguild/remark-npm2yarn';
import remarkGfm from 'remark-gfm';
import { SKIP, visit } from 'unist-util-visit';
import type { Heading, HeadingData, Root } from 'mdast';
import { phrasing } from 'mdast-util-phrasing';
import type { VFileWithOutput } from 'unified';
import {
transformerNotationDiff,
transformerNotationHighlight,
transformerNotationFocus,
transformerNotationWordHighlight,
transformerNotationErrorLevel,
} from '@shikijs/transformers';
import { remarkSearchData } from './search-data';

const EXPORT_FOOTER = 'export default ';

interface Section {
heading?: { id: string; depth: Heading['depth']; text: string };
content: string;
}

export async function mdLoader(source: string) {
const meta = grayMatter(source);
const file = await compile(source, {
Expand Down Expand Up @@ -75,67 +68,7 @@ export async function mdLoader(source: string) {
},
},
],
() => {
return (root: Root, vFile: VFileWithOutput<any>) => {
const sections: Section[] = [];

let currentSection: Section | undefined;
let previousParentNode: any;

visit(root, (node, _, parent) => {
if (node.type === 'heading') {
if (currentSection) {
sections.push(currentSection);
}

const heading = node;
const id = (heading.data as HeadingData & { id: string })?.id;
const depth = heading.depth;
let text = '';
visit(heading, ['text', 'inlineCode'], (hChild: any) => {
text += hChild.value;
});

currentSection = {
heading: {
id,
depth,
text,
},
content: '',
};

return SKIP;
}

const types = ['text', 'inlineCode'];

if (types.includes(node.type)) {
if (!currentSection) {
currentSection = {
heading: undefined,
content: '',
};
}

if (
previousParentNode &&
previousParentNode !== parent &&
!phrasing(previousParentNode)
) {
currentSection.content += ' ';
}
currentSection.content += (node as any).value;

previousParentNode = parent;
}
});

if (currentSection) sections.push(currentSection);

vFile.data = { ...vFile.data, sections };
};
},
remarkSearchData,
],
rehypePlugins: [
rehypeExtractExcerpt,
Expand Down Expand Up @@ -168,6 +101,10 @@ export async function mdLoader(source: string) {
excerpt: file.data.excerpt,
};

const firstHeading = (file.data.headings as Heading[]).find(
(h) => h.depth === 1,
);

const script = `
import { createMdxPage } from "@neato/guider/client";
Expand All @@ -185,6 +122,7 @@ export async function mdLoader(source: string) {
script,
searchData: {
sections: file.data.sections,
pageTitle: meta.data?.title ?? firstHeading?.value ?? undefined,
},
};
}
75 changes: 75 additions & 0 deletions packages/guider/src/webpack/loader/search-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { SKIP, visit } from 'unist-util-visit';
import type { Heading, Root } from 'mdast';
import { phrasing } from 'mdast-util-phrasing';
import type { VFileWithOutput } from 'unified';

interface Section {
heading?: { id: string; depth: Heading['depth']; text: string };
content: string;
}

declare module 'mdast' {
interface HeadingData {
id: string;
}
}

export function remarkSearchData() {
return (root: Root, vFile: VFileWithOutput<any>) => {
const sections: Section[] = [];

let currentSection: Section | undefined;
let previousParentNode: any;

visit(root, (node, _, parent) => {
if (node.type === 'heading') {
if (currentSection) {
sections.push(currentSection);
}

const heading = node;
const id = heading.data?.id ?? '';
const depth = heading.depth;
let text = '';
visit(heading, ['text', 'inlineCode'], (hChild: any) => {
text += hChild.value;
});

currentSection = {
heading: {
id,
depth,
text,
},
content: '',
};

return SKIP;
}

if (node.type === 'text' || node.type === 'inlineCode') {
if (!currentSection) {
currentSection = {
heading: undefined,
content: '',
};
}

if (
previousParentNode &&
previousParentNode !== parent &&
!phrasing(previousParentNode)
) {
currentSection.content += ' ';
}
currentSection.content += node.value;

previousParentNode = parent;
}
});

if (currentSection) sections.push(currentSection);

vFile.data = { ...vFile.data, sections };
};
}

0 comments on commit fac8707

Please sign in to comment.