mirror of
https://github.com/akveo/ngx-admin.git
synced 2025-12-16 23:40:14 +01:00
fix(docs): async md load
This commit is contained in:
parent
e77829a465
commit
0026c02fdf
7 changed files with 73 additions and 33 deletions
|
|
@ -195,6 +195,7 @@
|
||||||
"tsConfig": "docs/tsconfig.app.json",
|
"tsConfig": "docs/tsconfig.app.json",
|
||||||
"polyfills": "docs/polyfills.ts",
|
"polyfills": "docs/polyfills.ts",
|
||||||
"assets": [
|
"assets": [
|
||||||
|
"docs/articles",
|
||||||
"docs/assets",
|
"docs/assets",
|
||||||
"docs/404.html",
|
"docs/404.html",
|
||||||
"docs/favicon.png",
|
"docs/favicon.png",
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ import {
|
||||||
} from '@angular/core';
|
} from '@angular/core';
|
||||||
import { takeWhile, map } from 'rxjs/operators';
|
import { takeWhile, map } from 'rxjs/operators';
|
||||||
import { ActivatedRoute } from '@angular/router';
|
import { ActivatedRoute } from '@angular/router';
|
||||||
import { of as observableOf, combineLatest } from 'rxjs';
|
import { combineLatest, Observable } from 'rxjs';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'ngx-page-toc',
|
selector: 'ngx-page-toc',
|
||||||
|
|
@ -35,11 +35,11 @@ export class NgxPageTocComponent implements OnDestroy {
|
||||||
items: any[];
|
items: any[];
|
||||||
|
|
||||||
@Input()
|
@Input()
|
||||||
set toc(value) {
|
set toc(value: Observable<any[]>) {
|
||||||
combineLatest(
|
combineLatest([
|
||||||
observableOf(value || []),
|
value,
|
||||||
this.activatedRoute.fragment,
|
this.activatedRoute.fragment,
|
||||||
)
|
])
|
||||||
.pipe(
|
.pipe(
|
||||||
takeWhile(() => this.alive),
|
takeWhile(() => this.alive),
|
||||||
map(([toc, fragment]) => {
|
map(([toc, fragment]) => {
|
||||||
|
|
|
||||||
13
docs/app/@theme/services/article.service.ts
Normal file
13
docs/app/@theme/services/article.service.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { HttpClient } from '@angular/common/http';
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class NgxArticleService {
|
||||||
|
|
||||||
|
constructor(private http: HttpClient) { }
|
||||||
|
|
||||||
|
getArticle(source: string): Observable<string> {
|
||||||
|
return this.http.get(`articles/${source}`, { responseType: 'text' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -18,6 +18,7 @@ import { NgxCodeLoaderService } from './code-loader.service';
|
||||||
import { NgxStylesService } from './styles.service';
|
import { NgxStylesService } from './styles.service';
|
||||||
import { NgxIframeCommunicatorService } from './iframe-communicator.service';
|
import { NgxIframeCommunicatorService } from './iframe-communicator.service';
|
||||||
import { NgxVisibilityService } from './visibility.service';
|
import { NgxVisibilityService } from './visibility.service';
|
||||||
|
import { NgxArticleService } from './article.service';
|
||||||
|
|
||||||
|
|
||||||
export const ngxLandingServices = [
|
export const ngxLandingServices = [
|
||||||
|
|
@ -35,4 +36,5 @@ export const ngxLandingServices = [
|
||||||
NgxStylesService,
|
NgxStylesService,
|
||||||
NgxIframeCommunicatorService,
|
NgxIframeCommunicatorService,
|
||||||
NgxVisibilityService,
|
NgxVisibilityService,
|
||||||
|
NgxArticleService,
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,13 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Inject, Injectable } from '@angular/core';
|
import { Inject, Injectable } from '@angular/core';
|
||||||
|
import { map } from 'rxjs/operators';
|
||||||
|
import { combineLatest, Observable, of } from 'rxjs';
|
||||||
|
|
||||||
import { NgxTabbedService } from './tabbed.service';
|
import { NgxTabbedService } from './tabbed.service';
|
||||||
import { NgxTextService } from './text.service';
|
import { NgxTextService } from './text.service';
|
||||||
import { DOCS, STRUCTURE } from '../../app.options';
|
import { DOCS, STRUCTURE } from '../../app.options';
|
||||||
|
import { NgxArticleService } from './article.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class NgxStructureService {
|
export class NgxStructureService {
|
||||||
|
|
@ -17,6 +20,7 @@ export class NgxStructureService {
|
||||||
|
|
||||||
constructor(private textService: NgxTextService,
|
constructor(private textService: NgxTextService,
|
||||||
private tabbedService: NgxTabbedService,
|
private tabbedService: NgxTabbedService,
|
||||||
|
private articleService: NgxArticleService,
|
||||||
@Inject(STRUCTURE) structure,
|
@Inject(STRUCTURE) structure,
|
||||||
@Inject(DOCS) docs) {
|
@Inject(DOCS) docs) {
|
||||||
this.prepared = this.prepareStructure(structure, docs);
|
this.prepared = this.prepareStructure(structure, docs);
|
||||||
|
|
@ -56,8 +60,9 @@ export class NgxStructureService {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item.block === 'markdown') {
|
if (item.block === 'markdown') {
|
||||||
item.children = this.textService.mdToSectionsHTML(
|
item.sections = this.articleService.getArticle(item.source).pipe(
|
||||||
require(`raw-loader!../../../articles/${item.source}`).default);
|
map((article) => this.textService.mdToSectionsHTML(article)),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item.children) {
|
if (item.children) {
|
||||||
|
|
@ -113,39 +118,43 @@ export class NgxStructureService {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
protected prepareToc(item: any) {
|
protected prepareToc(item: any): Observable<any[]> {
|
||||||
return item.children.reduce((acc: any[], child: any) => {
|
const tocList = item.children.reduce((acc: Observable<any>[], child: any) => {
|
||||||
if (child.block === 'markdown') {
|
if (child.block === 'markdown') {
|
||||||
return acc.concat(this.getTocForMd(child));
|
return [...acc, this.getTocForMd(child)];
|
||||||
} else if (child.block === 'tabbed') {
|
}
|
||||||
return acc.concat(this.getTocForTabbed(child));
|
if (child.block === 'tabbed') {
|
||||||
} else if (child.block === 'component') {
|
return [...acc, this.getTocForTabbed(child)];
|
||||||
acc.push(this.getTocForComponent(child));
|
}
|
||||||
|
if (child.block === 'component') {
|
||||||
|
return [...acc, this.getTocForComponent(child)];
|
||||||
}
|
}
|
||||||
return acc;
|
return acc;
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
return combineLatest([...tocList]).pipe(map((toc) => [].concat(...toc)));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected getTocForMd(block: any) {
|
protected getTocForMd(block: any): Observable<any[]> {
|
||||||
return block.children.map((section: any) => ({
|
return (block.sections as Observable<any[]>).pipe(
|
||||||
title: section.title,
|
map((item) => item.map((val) => ({
|
||||||
fragment: section.fragment,
|
title: val.title,
|
||||||
}
|
fragment: val.fragment,
|
||||||
));
|
}))),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected getTocForComponent(block: any) {
|
protected getTocForComponent(block: any): Observable<any[]> {
|
||||||
return {
|
return of([{
|
||||||
title: block.source.name,
|
title: block.source.name,
|
||||||
fragment: block.source.slag,
|
fragment: block.source.slag,
|
||||||
};
|
}]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected getTocForTabbed(block: any) {
|
protected getTocForTabbed(block: any): Observable<any[]> {
|
||||||
return block.children.map((component: any) => ({
|
return of(block.children.map((component: any) => ({
|
||||||
title: component.name,
|
title: component.name,
|
||||||
fragment: this.textService.createSlag(component.name),
|
fragment: this.textService.createSlag(component.name),
|
||||||
}
|
})));
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,19 +5,34 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
|
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
|
||||||
|
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'ngx-md-block',
|
selector: 'ngx-md-block',
|
||||||
template: `
|
template: `
|
||||||
<nb-card *ngFor="let section of source;" [ngxFragment]="section.fragment">
|
<nb-card *ngFor="let section of content;" [ngxFragment]="section.fragment">
|
||||||
<nb-card-body>
|
<nb-card-body>
|
||||||
<div [innerHtml]="section.html"></div>
|
<div [innerHtml]="getTemplate(section.html)"></div>
|
||||||
</nb-card-body>
|
</nb-card-body>
|
||||||
</nb-card>
|
</nb-card>
|
||||||
`,
|
`,
|
||||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||||
})
|
})
|
||||||
export class NgxMdBLockComponent {
|
export class NgxMdBLockComponent {
|
||||||
|
@Input() content: MdChildren[] = [];
|
||||||
|
|
||||||
@Input() source: string;
|
constructor(private readonly domSanitizer: DomSanitizer) {
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: create NbDOMPurifyPipe
|
||||||
|
getTemplate(content: string): SafeHtml {
|
||||||
|
return this.domSanitizer.bypassSecurityTrustHtml(content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface MdChildren {
|
||||||
|
fragment: string;
|
||||||
|
html: string;
|
||||||
|
source: string;
|
||||||
|
title: string;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
|
|
||||||
<ng-container *ngFor="let block of currentItem?.children">
|
<ng-container *ngFor="let block of currentItem?.children">
|
||||||
<ng-container [ngSwitch]="block.block">
|
<ng-container [ngSwitch]="block.block">
|
||||||
<ngx-md-block *ngSwitchCase="'markdown'" [source]="block.children"></ngx-md-block>
|
<ngx-md-block *ngSwitchCase="'markdown'" [content]="block.sections | async"></ngx-md-block>
|
||||||
<ngx-component-block *ngSwitchCase="'component'" [source]="block.source"></ngx-component-block>
|
<ngx-component-block *ngSwitchCase="'component'" [source]="block.source"></ngx-component-block>
|
||||||
<ngx-tabbed-block *ngSwitchCase="'tabbed'" [source]="block.children" [tabs]="currentItem.tabs"></ngx-tabbed-block>
|
<ngx-tabbed-block *ngSwitchCase="'tabbed'" [source]="block.children" [tabs]="currentItem.tabs"></ngx-tabbed-block>
|
||||||
<ngx-theme-block *ngSwitchCase="'theme'" [block]="block"></ngx-theme-block>
|
<ngx-theme-block *ngSwitchCase="'theme'" [block]="block"></ngx-theme-block>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue