Skip to content

Commit

Permalink
add logic to add pagefind weight for pages and tags (heading, paragra…
Browse files Browse the repository at this point in the history
…ph, connectorDetailsHeader)
  • Loading branch information
aniketkatkar97 committed Feb 29, 2024
1 parent e50086e commit c8fa0d6
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface ConnectorDetailsHeaderProps {
platform: string;
availableFeatures: Array<string>;
unavailableFeatures: Array<string>;
searchWeight: string;
}
13 changes: 12 additions & 1 deletion components/ConnectorDetailsHeader/ConnectorDetailsHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,29 @@ function ConnectorDetailsHeader({
platform,
availableFeatures,
unavailableFeatures,
searchWeight,
}: Readonly<ConnectorDetailsHeaderProps>) {
const showSubHeading = useMemo(
() => !isEmpty(availableFeatures) || !isEmpty(unavailableFeatures),
[availableFeatures, unavailableFeatures]
);

let otherProps = {};

if (searchWeight) {
otherProps = {
["data-pagefind-weight"]: searchWeight,
};
}

return (
<div className={styles.Container}>
<div className={styles.Heading}>
<div className="flex items-center gap-3">
<div className={styles.ImageContainer}>{getConnectorImage(name)}</div>
<div className={styles.ConnectorName}>{name}</div>
<div className={styles.ConnectorName} {...otherProps}>
{name}
</div>
{getStageBadge(stage)}
</div>
<div className={styles.PlatformDetails}>
Expand Down
9 changes: 9 additions & 0 deletions components/Heading/Heading.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ReactNode } from "react";

export interface HeadingProps {
children: ReactNode;
id?: string;
level?: number;
className?: string;
searchWeight?: string;
}
23 changes: 21 additions & 2 deletions components/Heading/Heading.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import classNames from "classnames";
import { useState } from "react";
import { HeadingProps } from "./Heading.interface";
import styles from "./Heading.module.css";
import HeadingElement from "./HeadingElement";

export function Heading({ id = "", level = 1, children, className }) {
export function Heading({
id = "",
level = 1,
children,
className = "",
searchWeight,
}: Readonly<HeadingProps>) {
const [copied, setCopied] = useState(false);

const copyLinkUnbound = async () => {
Expand All @@ -14,14 +21,26 @@ export function Heading({ id = "", level = 1, children, className }) {
window.setTimeout(() => setCopied(false), 2000);
};

let otherProps = {};

if (searchWeight) {
otherProps = {
["data-pagefind-weight"]: searchWeight,
};
}

return (
<div
className={classNames(className, styles.HeaderLink, {
[styles.H1Element]: level === 1,
})}
>
<a id={id} className={styles.HashLink} />
<HeadingElement className={styles.HeaderElement} level={level}>
<HeadingElement
className={styles.HeaderElement}
level={level}
{...otherProps}
>
{children}
</HeadingElement>
{copied ? (
Expand Down
45 changes: 37 additions & 8 deletions components/Heading/HeadingElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,50 @@ export default function HeadingElement({
level,
className = "",
children,
}: Props) {
...otherProps
}: Readonly<Props>) {
switch (level) {
case 1:
return <h1 className={className}>{children}</h1>;
return (
<h1 className={className} {...otherProps}>
{children}
</h1>
);
case 2:
return <h2 className={className}>{children}</h2>;
return (
<h2 className={className} {...otherProps}>
{children}
</h2>
);
case 3:
return <h3 className={className}>{children}</h3>;
return (
<h3 className={className} {...otherProps}>
{children}
</h3>
);
case 4:
return <h4 className={className}>{children}</h4>;
return (
<h4 className={className} {...otherProps}>
{children}
</h4>
);
case 5:
return <h5 className={className}>{children}</h5>;
return (
<h5 className={className} {...otherProps}>
{children}
</h5>
);
case 6:
return <h6 className={className}>{children}</h6>;
return (
<h6 className={className} {...otherProps}>
{children}
</h6>
);
default:
return <h6 className={className}>{children}</h6>;
return (
<h6 className={className} {...otherProps}>
{children}
</h6>
);
}
}
4 changes: 3 additions & 1 deletion components/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ export default function Search({
setIsLoading(true);
if (window[`pageFind${docVersion}`]) {
const search = await window[`pageFind${docVersion}`].search(
isEmpty(searchValue) ? "releases" : searchValue
// To show results for "releases" as a default suggestions when no search text is present
isEmpty(searchValue) ? "releases" : searchValue,
{ sort: { weight: "desc" } }
);

// Select only top 20 results
Expand Down
6 changes: 6 additions & 0 deletions components/common/Paragraph/Paragraph.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ReactNode } from "react";

export interface ParagraphProps {
children: ReactNode;
searchWeight?: string;
}
14 changes: 14 additions & 0 deletions components/common/Paragraph/Paragraph.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ParagraphProps } from "./Paragraph.interface";

function Paragraph({ searchWeight, children }: ParagraphProps) {
let otherProps = {};

if (searchWeight) {
otherProps = {
["data-pagefind-weight"]: searchWeight,
};
}
return <p {...otherProps}>{children}</p>;
}

export default Paragraph;
2 changes: 2 additions & 0 deletions lib/markdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Image from "../components/common/Image/Image";
import InlineCallout from "../components/common/InlineCallout/InlineCallout";
import InlineCalloutContainer from "../components/common/InlineCallout/InlineCalloutContainer";
import Note from "../components/common/Note/Note";
import Paragraph from "../components/common/Paragraph/Paragraph";
import MultiTablesWrapper from "../components/common/Table/MultiTablesWrapper";
import Table from "../components/common/Table/Table";
import Tile from "../components/common/Tiles/Tile/Tile";
Expand All @@ -51,6 +52,7 @@ export const components = {
CodePreview,
Code,
Heading,
Paragraph,
Image,
Step,
StepsContainer,
Expand Down
1 change: 1 addition & 0 deletions markdoc/nodes/heading.markdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const heading = {
level: { type: Number, required: true, default: 1 },
className: { type: String },
children: { type: String },
searchWeight: { type: String },
},
transform(node, config) {
const attributes = node.transformAttributes(config);
Expand Down
4 changes: 3 additions & 1 deletion markdoc/nodes/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* Use this file to export your markdoc nodes */
export * from "./code.markdoc";
export * from "./fence.markdoc";
export * from "./link.markdoc";
export * from "./heading.markdoc";
export * from "./link.markdoc";
export * from "./paragraph.markdoc";
export * from "./table/table.markdoc";

6 changes: 6 additions & 0 deletions markdoc/nodes/paragraph.markdoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const paragraph = {
render: "Paragraph",
attributes: {
searchWeight: { type: String },
},
};
1 change: 1 addition & 0 deletions markdoc/tags/connectorDetailsHeader.markdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ export const connectorDetailsHeader = {
type: Array<string>,
description: "List of unavailable features for the connector.",
},
searchWeight: { type: String },
},
};
2 changes: 2 additions & 0 deletions pages/[version]/[...slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ export default function Article({
<ErrorBoundary>
<div
className="hidden"
data-pagefind-sort="weight[data-weight]"
data-pagefind-meta="title"
data-weight={metaData.weight ?? "0"}
id="article-title-metadata"
>
{metaData.title}
Expand Down

0 comments on commit c8fa0d6

Please sign in to comment.