Skip to content

Commit 0fd059f

Browse files
committed
working on correctHeadings
1 parent 400b6b1 commit 0fd059f

14 files changed

Lines changed: 66 additions & 33 deletions

File tree

src/app/book/book.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { ElementInputDirective } from './directives/element-input.directive';
2828

2929
import { createCustomElement } from '@angular/elements';
3030

31-
import { preset } from './gbr-preset/index';
31+
import { preset } from '../gbr-preset';
3232
// import { DocspaStackblitzModule } from '@swimlane/docspa-stackblitz';
3333
// import * as squeezeParagraphs from 'remark-squeeze-paragraphs';
3434

src/app/book/markdown-elements/md-link.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,17 @@ import { RouterService } from '../services/router.service';
2424
export class MdLinkComponent implements OnChanges, AfterContentInit {
2525
static readonly is = 'md-link';
2626

27-
@Input()
28-
link: string;
27+
@Input() link: string;
2928

30-
@Input('aria-hidden')
31-
ariaHidden: any;
29+
@Input('aria-hidden') ariaHidden: any;
3230

33-
@Input()
34-
download: boolean;
31+
@Input() download: boolean;
3532

36-
@Input()
37-
klass: boolean;
33+
@Input() klass: boolean;
3834

39-
@Input()
40-
routerLink: string | string[];
35+
@Input() routerLink: string | string[];
4136

42-
@Input()
43-
fragment: string;
37+
@Input() fragment: string;
4438

4539
@ViewChild(TemplateRef, { static: true }) private template: TemplateRef<void>;
4640

@@ -53,6 +47,7 @@ export class MdLinkComponent implements OnChanges, AfterContentInit {
5347
get rootPath () {
5448
return this.routerService.root;
5549
}
50+
5651
ngAfterContentInit() {
5752
// Moving link outside of component
5853
this.vcRef.createEmbeddedView(this.template);

src/app/book/markdown/markdown.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import unified from 'unified';
1414
import remark from 'remark';
1515
import remark2rehype from 'remark-rehype';
1616
import rehypeStringify from 'rehype-stringify';
17+
import gfm from 'remark-gfm';
1718
import raw from 'rehype-raw';
1819
import frontmatter from 'remark-frontmatter';
1920
import slug from 'remark-slug';
@@ -22,7 +23,7 @@ import toString from 'mdast-util-to-string';
2223
import strip from 'remark-strip-html';
2324
import sectionize from 'remark-sectionize';
2425

25-
import { getTitle } from '../gbr-preset/index';
26+
import { getTitle } from '../../gbr-preset';
2627
import { tocPlugin } from '../plugins/toc';
2728
import { removeNodesPlugin } from '../plugins/remove';
2829
import { sectionPlugin } from '../plugins/sections';

src/app/book/services/search.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import slug from 'remark-slug';
1212
import { links, images } from '../shared/links';
1313
import frontmatter from 'remark-frontmatter';
1414

15-
import { getTitle } from '../gbr-preset/index';
15+
import { getTitle } from '../../gbr-preset';
1616
import { FetchService } from './fetch.service';
1717
import { LocationService } from './location.service';
1818
import { MarkdownService } from '../markdown/markdown.service';

src/app/book/shared/links.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const links = (settings: { locationService: LocationService }): Transform
4747
let [routerLink = '', fragment] = node.url.split('#');
4848
fragment = fragment ? fragment.replace(/^#/, '') : undefined;
4949

50-
node.data.hProperties.link = routerLink;
50+
node.data.hProperties.link = routerLink.replace(/\/$/, '');
5151
node.data.hProperties.fragment = fragment;
5252
node.data.hProperties.source = vfile.history[0];
5353
node.data.hProperties.klass = node.data.hProperties.class;

src/app/book/gbr-preset/plugins/frontmatter.ts renamed to src/app/gbr-preset/plugins/frontmatter.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import visit from 'unist-util-visit';
22
import toString from 'mdast-util-to-string';
3-
import { Root, Heading } from 'mdast';
3+
import { Root, Heading, Paragraph } from 'mdast';
44
import { VFile as _VFile } from 'vfile';
55
import { Transformer, Attacher, Settings } from 'unified';
66

@@ -23,7 +23,10 @@ export function readMatter(): Transformer {
2323

2424
export function getTitle(): Transformer {
2525
return (tree: Root, file: VFile) => {
26-
if(tree.children.findIndex(_ => _.type === 'heading') < 0 && !(/^\_/.test(file.basename))){
26+
if(tree.children.findIndex(_ => _.type === 'heading') < 0
27+
&& !(/^\_/.test(file.basename))
28+
&& tree.children.length > 0)
29+
{
2730
const heading: Heading = {
2831
type: 'heading',
2932
depth: 1,
@@ -42,3 +45,26 @@ export function getTitle(): Transformer {
4245
});
4346
};
4447
}
48+
49+
export function correctHeadings(): Transformer {
50+
return (tree: Root, file: VFile) => {
51+
return visit(tree, 'paragraph', (node: Paragraph, index: number, parent: any) => {
52+
53+
const text: string = node.children[0].value as string;
54+
let re = new RegExp(/^(\#){1,3}/);
55+
if(re.test(text)){
56+
const hashes = text.match(/(\#){1,3}/)[0];
57+
const _text = text.replace(hashes, '');
58+
59+
const heading: Heading = {
60+
type: 'heading',
61+
depth: hashes.length as 2 | 1 | 3 | 4 | 5 | 6,
62+
children: [{type: 'text', value: _text}]
63+
};
64+
65+
parent.children.splice(index, 1, heading);
66+
}
67+
68+
});
69+
};
70+
}
File renamed without changes.

0 commit comments

Comments
 (0)