Skip to content

Commit

Permalink
fix(nx-dev): adjust scroll offset for headings on docs and blog conta…
Browse files Browse the repository at this point in the history
…iner

(cherry picked from commit 90e29f0)
  • Loading branch information
juristr authored and FrozenPandaz committed Aug 1, 2024
1 parent fd9f557 commit c4674f4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions nx-dev/feature-doc-viewer/src/lib/doc-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function DocViewer({
document.content.toString(),
{
filePath: document.filePath,
headingClass: 'scroll-mt-8',
}
);

Expand Down
1 change: 1 addition & 0 deletions nx-dev/ui-blog/src/lib/blog-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export async function generateMetadata({ post }: BlogDetailsProps) {
export function BlogDetails({ post }: BlogDetailsProps) {
const { node } = renderMarkdown(post.content, {
filePath: post.filePath ?? '',
headingClass: 'scroll-mt-20',
});

const formattedDate = new Date(post.date).toLocaleDateString('en-US', {
Expand Down
14 changes: 9 additions & 5 deletions nx-dev/ui-markdoc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { load as yamlLoad } from '@zkochan/js-yaml';
import React, { ReactNode } from 'react';
import { Heading } from './lib/nodes/heading.component';
import { heading } from './lib/nodes/heading.schema';
import { getHeadingSchema } from './lib/nodes/heading.schema';
import { getImageSchema } from './lib/nodes/image.schema';
import { CustomLink } from './lib/nodes/link.component';
import { link } from './lib/nodes/link.schema';
Expand Down Expand Up @@ -63,12 +63,13 @@ import { VideoPlayer, videoPlayer } from './lib/tags/video-player.component';
export { GithubRepository } from './lib/tags/github-repository.component';

export const getMarkdocCustomConfig = (
documentFilePath: string
documentFilePath: string,
headingClass: string = ''
): { config: any; components: any } => ({
config: {
nodes: {
fence,
heading,
heading: getHeadingSchema(headingClass),
image: getImageSchema(documentFilePath),
link,
},
Expand Down Expand Up @@ -156,14 +157,17 @@ export const extractFrontmatter = (

export const renderMarkdown: (
documentContent: string,
options: { filePath: string }
options: { filePath: string; headingClass?: string }
) => {
metadata: Record<string, any>;
node: ReactNode;
treeNode: RenderableTreeNode;
} = (documentContent, options = { filePath: '' }) => {
const ast = parseMarkdown(documentContent);
const configuration = getMarkdocCustomConfig(options.filePath);
const configuration = getMarkdocCustomConfig(
options.filePath,
options.headingClass
);
const treeNode = transform(ast, configuration.config);

return {
Expand Down
6 changes: 3 additions & 3 deletions nx-dev/ui-markdoc/src/lib/nodes/heading.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function generateID(
.replace(/\s+/g, '-');
}

export const heading: Schema = {
export const getHeadingSchema = (headingClass: string): Schema => ({
render: 'Heading',
children: ['inline'],
attributes: {
Expand All @@ -58,8 +58,8 @@ export const heading: Schema = {
return new Tag(
this.render,
// `h${node.attributes['level']}`,
{ ...attributes, id },
{ ...attributes, id, className: headingClass },
children
);
},
};
});

0 comments on commit c4674f4

Please sign in to comment.