-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
markdown component fixes anchor navigation
- Loading branch information
Showing
10 changed files
with
81 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<span [innerHTML]="content | marked | async" (click)="onClick($event)"></span> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { MarkdownComponent } from './markdown.component'; | ||
|
||
describe('MarkdownComponent', () => { | ||
let component: MarkdownComponent; | ||
let fixture: ComponentFixture<MarkdownComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [MarkdownComponent] | ||
}) | ||
.compileComponents(); | ||
|
||
fixture = TestBed.createComponent(MarkdownComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Component, Input, inject } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { Router } from '@angular/router'; | ||
import { MarkedPipe } from '../../../pipes/marked.pipe'; | ||
|
||
@Component({ | ||
selector: 'app-markdown', | ||
standalone: true, | ||
templateUrl: './markdown.component.html', | ||
styleUrl: './markdown.component.scss', | ||
imports: [CommonModule, MarkedPipe], | ||
}) | ||
export class MarkdownComponent { | ||
private _router = inject(Router); | ||
@Input({ required: true }) content!: string; | ||
|
||
// https://stackoverflow.com/questions/51764517/use-angular-router-inside-markdown-links | ||
public onClick(e: MouseEvent): void { | ||
const srcElem = e.target; | ||
if (srcElem instanceof HTMLAnchorElement) { | ||
const href = srcElem.href; | ||
const isLocalLink = href.startsWith(srcElem.baseURI); | ||
if (isLocalLink) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
|
||
const url = new URL(href); | ||
const fragment = url.hash ? url.hash.substring(1) : undefined; | ||
this._router.navigate([url.pathname], { fragment }); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters