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): statically highlight lines in code #19703

Merged
merged 1 commit into from
Oct 18, 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
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