Skip to content

Commit f04abe7

Browse files
committed
add js package codeblocks
1 parent d886d88 commit f04abe7

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

src/components/codeBlock/index.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use client';
22

3-
import {useEffect, useRef, useState} from 'react';
3+
import {useContext, useEffect, useRef, useState} from 'react';
44
import {Clipboard} from 'react-feather';
55

66
import styles from './code-blocks.module.scss';
77

8+
import {CodeContext} from '../codeContext';
89
import {makeKeywordsClickable} from '../codeKeywords';
910

1011
export interface CodeBlockProps {
@@ -17,6 +18,7 @@ export interface CodeBlockProps {
1718
export function CodeBlock({filename, language, children}: CodeBlockProps) {
1819
const [showCopied, setShowCopied] = useState(false);
1920
const codeRef = useRef<HTMLDivElement>(null);
21+
const codeContext = useContext(CodeContext);
2022

2123
// Show the copy button after js has loaded
2224
// otherwise the copy button will not work
@@ -57,7 +59,9 @@ export function CodeBlock({filename, language, children}: CodeBlockProps) {
5759
<div className={styles.copied} style={{opacity: showCopied ? 1 : 0}}>
5860
Copied
5961
</div>
60-
<div ref={codeRef}>{makeKeywordsClickable(children)}</div>
62+
<div ref={codeRef}>
63+
{makeKeywordsClickable(children, codeContext?.currentJsPackage)}
64+
</div>
6165
</div>
6266
);
6367
}

src/components/codeContext.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ type CodeContextType = {
105105
];
106106
storedCodeSelection: SelectedCodeTabs;
107107
updateCodeSelection: (selection: CodeSelection) => void;
108+
currentJsPackage?: string;
108109
};
109110

110111
export const CodeContext = createContext<CodeContextType | null>(null);
@@ -293,7 +294,13 @@ const getLocallyStoredSelections = (): SelectedCodeTabs => {
293294
return {};
294295
};
295296

296-
export function CodeContextProvider({children}: {children: React.ReactNode}) {
297+
export function CodeContextProvider({
298+
children,
299+
currentJsPackage,
300+
}: {
301+
children: React.ReactNode;
302+
currentJsPackage?: string;
303+
}) {
297304
const [codeKeywords, setCodeKeywords] = useState(cachedCodeKeywords ?? DEFAULTS);
298305
const [isLoading, setIsLoading] = useState<boolean>(cachedCodeKeywords ? false : true);
299306
const [storedCodeSelection, setStoredCodeSelection] = useState<SelectedCodeTabs>({});
@@ -337,6 +344,7 @@ export function CodeContextProvider({children}: {children: React.ReactNode}) {
337344
const sharedKeywordSelection = useState<Record<string, number>>({});
338345

339346
const result: CodeContextType = {
347+
currentJsPackage,
340348
codeKeywords,
341349
storedCodeSelection,
342350
updateCodeSelection,

src/components/codeKeywords/codeKeywords.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,29 @@ export const KEYWORDS_REGEX = /\b___(?:([A-Z_][A-Z0-9_]*)\.)?([A-Z_][A-Z0-9_]*)_
99

1010
export const ORG_AUTH_TOKEN_REGEX = /___ORG_AUTH_TOKEN___/g;
1111

12+
export const JS_PACKAGE_REGEX = /___JS_PACKAGE___/g;
13+
1214
type ChildrenItem = ReturnType<typeof Children.toArray>[number] | React.ReactNode;
1315

14-
export function makeKeywordsClickable(children: React.ReactNode) {
16+
export function makeKeywordsClickable(
17+
children: React.ReactNode,
18+
currentJsPackage?: string
19+
) {
1520
const items = Children.toArray(children);
1621

1722
return items.reduce((arr: ChildrenItem[], child) => {
1823
if (typeof child !== 'string') {
1924
const updatedChild = cloneElement(
2025
child as ReactElement,
2126
{},
22-
makeKeywordsClickable((child as ReactElement).props.children)
27+
makeKeywordsClickable((child as ReactElement).props.children, currentJsPackage)
2328
);
2429
arr.push(updatedChild);
2530
return arr;
2631
}
27-
if (ORG_AUTH_TOKEN_REGEX.test(child)) {
32+
if (JS_PACKAGE_REGEX.test(child)) {
33+
resolveJsPackage(arr, child, currentJsPackage);
34+
} else if (ORG_AUTH_TOKEN_REGEX.test(child)) {
2835
makeOrgAuthTokenClickable(arr, child);
2936
} else if (KEYWORDS_REGEX.test(child)) {
3037
makeProjectKeywordsClickable(arr, child);
@@ -36,6 +43,12 @@ export function makeKeywordsClickable(children: React.ReactNode) {
3643
}, [] as ChildrenItem[]);
3744
}
3845

46+
function resolveJsPackage(arr: ChildrenItem[], str: string, currentGuide?: string) {
47+
runRegex(arr, str, JS_PACKAGE_REGEX, lastIndex => (
48+
<span key={`js-package-${lastIndex}`}>{`@sentry/${currentGuide}`}</span>
49+
));
50+
}
51+
3952
function makeOrgAuthTokenClickable(arr: ChildrenItem[], str: string) {
4053
runRegex(arr, str, ORG_AUTH_TOKEN_REGEX, lastIndex => (
4154
<OrgAuthTokenCreator key={`org-token-${lastIndex}`} />

src/components/docPage/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ export function DocPage({
5555

5656
const leafNode = nodeForPath(rootNode, unversionedPath);
5757

58+
const currentJsPackage =
59+
currentGuide?.sdk?.split('.').at(-1) || currentPlatform?.sdk?.split('.').at(-1);
60+
5861
return (
5962
<div className="tw-app">
6063
<Header pathname={pathname} searchPlatforms={searchPlatforms} />
@@ -83,7 +86,9 @@ export function DocPage({
8386
</hgroup>
8487
{/* This exact id is important for Algolia indexing */}
8588
<div id="main">
86-
<CodeContextProvider>{children}</CodeContextProvider>
89+
<CodeContextProvider currentJsPackage={currentJsPackage}>
90+
{children}
91+
</CodeContextProvider>
8792
</div>
8893

8994
<div className="grid grid-cols-2 gap-4 not-prose mt-16">

0 commit comments

Comments
 (0)