Skip to content

Commit

Permalink
feat(nx-dev): statically highlight lines in code (#19703)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacplmann authored Oct 18, 2023
1 parent 472dbc3 commit 5fd0097
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
12 changes: 12 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ For example:
[This will highlight the first group.](#first)
```

You can also statically highlight a set of lines (the user won't be able to change what is highlighted):

````
‎```javascript {% highlightLines=[2,3] %}
‎ const code = "goes here";
‎ This is highlighted
‎ This is also highlighted
‎ This is not highlighted
‎ Neither is this
‎```
````

#### Terminal command

To display a terminal command, use:
Expand Down
10 changes: 5 additions & 5 deletions docs/shared/angular-tutorial/angular-monorepo.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ export * from './lib/product-list/product-list.component';

We're ready to import it into our main application now. First (if you haven't already), let's set up the Angular router. Configure it in the `app.config.ts`.

```ts {% fileName="apps/angular-store/src/app/app.config.ts" %}
```ts {% fileName="apps/angular-store/src/app/app.config.ts" highlightLines=[2,3,4,5,6,9] %}
import { ApplicationConfig } from '@angular/core';
import {
provideRouter,
Expand All @@ -425,7 +425,7 @@ And in `app.component.html`:

Then we can add the `ProductListComponent` component to our `app.routes.ts` and render it via the routing mechanism whenever a user hits the `/products` route.

```ts {% fileName="apps/angular-store/src/app/app.routes.ts" %}
```ts {% fileName="apps/angular-store/src/app/app.routes.ts" highlightLines=[10,11,12,13,14] %}
import { Route } from '@angular/router';
import { NxWelcomeComponent } from './nx-welcome.component';

Expand Down Expand Up @@ -454,7 +454,7 @@ Let's apply the same for our `orders` library.

In the end, your `app.routes.ts` should look similar to this:

```ts {% fileName="apps/angular-store/src/app/app.routes.ts" %}
```ts {% fileName="apps/angular-store/src/app/app.routes.ts" highlightLines=[15,16,17,18,19] %}
import { Route } from '@angular/router';
import { NxWelcomeComponent } from './nx-welcome.component';

Expand All @@ -479,7 +479,7 @@ export const appRoutes: Route[] = [

Let's also show products in the `inventory` app.

```ts {% fileName="apps/inventory/src/app/app.component.ts" %}
```ts {% fileName="apps/inventory/src/app/app.component.ts" highlightLines=[2,6] %}
import { Component } from '@angular/core';
import { ProductListComponent } from '@angular-monorepo/products';

Expand Down Expand Up @@ -921,7 +921,7 @@ To enforce the rules, Nx ships with a custom ESLint rule. Open the `.eslintrc.ba

To test it, go to your `libs/products/src/lib/product-list/product-list.component.ts` file and import the `OrderListComponent` from the `orders` project:

```ts {% fileName="libs/products/src/lib/product-list/product-list.component.ts" %}
```ts {% fileName="libs/products/src/lib/product-list/product-list.component.ts" highlightLines=[4,5] %}
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';

Expand Down
7 changes: 6 additions & 1 deletion nx-dev/ui-markdoc/src/lib/nodes/fence.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ export function Fence({
path,
fileName,
lineGroups,
highlightLines,
language,
enableCopy,
}: {
children: string;
command: string;
path: string;
fileName: string;
highlightLines: number[];
lineGroups: Record<string, number[]>;
language: string;
enableCopy: boolean;
Expand All @@ -104,7 +106,10 @@ export function Fence({
const hash = decodeURIComponent(useUrlHash(''));

function lineNumberStyle(lineNumber: number) {
if (lineGroups[hash] && lineGroups[hash].includes(lineNumber)) {
if (
(highlightLines && highlightLines.includes(lineNumber)) ||
(lineGroups[hash] && lineGroups[hash].includes(lineNumber))
) {
return {
fontSize: 0,
display: 'inline-block',
Expand Down
1 change: 1 addition & 0 deletions nx-dev/ui-markdoc/src/lib/nodes/fence.schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const fence: Schema = {
content: { type: 'String', render: false, required: true },
language: { type: 'String' },
fileName: { type: 'String', default: '' },
highlightLines: { type: 'Array', default: [] },
lineGroups: { type: 'Object', default: {} },
command: { type: 'String', default: '' },
path: { type: 'String', default: '~/workspace' },
Expand Down

1 comment on commit 5fd0097

@vercel
Copy link

@vercel vercel bot commented on 5fd0097 Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.