Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nx-dev): allow ranges in fences highlighting #20202

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ You can also statically highlight a set of lines (the user won't be able to chan
‎```
````

You can also specify ranges like `highlightLines=[2,3,"8-10"]`.

#### Terminal command

To display a terminal command, use:
Expand Down
30 changes: 30 additions & 0 deletions nx-dev/ui-markdoc/src/lib/nodes/fence.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,32 @@ const useUrlHash = (initialValue: string) => {
return hash;
};

// pre-process the highlightLines to expand ranges like
// ["8-10", 19, 22] => [8,9,10,19,22]
function processHighlightLines(highlightLines: any): number[] {
const expandRange = (range: any) => {
const [start, end] = range.split('-').map(Number);
return Array.from({ length: end - start + 1 }, (_, i) => start + i);
};

// Process each item in the array
return (
highlightLines
.map((item: any) => {
if (typeof item === 'string' && item.includes('-')) {
return expandRange(item);
}
return Number(item);
})
.flat()
// remove duplicates
.filter(
(value: any, index: number, self: number[]) =>
self.indexOf(value) === index
)
);
}

export function Fence({
children,
command,
Expand All @@ -105,6 +131,10 @@ export function Fence({
const { push, asPath } = useRouter();
const hash = decodeURIComponent(useUrlHash(''));

if (highlightLines) {
highlightLines = processHighlightLines(highlightLines);
}

function lineNumberStyle(lineNumber: number) {
if (
(highlightLines && highlightLines.includes(lineNumber)) ||
Expand Down