【发布时间】:2018-05-12 12:16:24
【问题描述】:
我有一个角管:
import {Pipe, PipeTransform} from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import * as Remarkable from 'remarkable';
import * as toc from 'markdown-toc';
@Pipe({
name: 'MarkdownToc'
})
export class MarkdownTableOfContentsPipe implements PipeTransform {
constructor(private sanitized: DomSanitizer) {}
public transform(markdown: string) {
const toc_opts = {
linkify: function(tok, text, slug, options) {
const regex = /(.+\b)(.*)$/
slug = slug.replace(regex, function(str, g1) { return g1; });
tok.content = `[${text}](#${slug})`;
return tok;
}
}
const toc_md = new Remarkable('commonmark')
.use(toc.plugin(toc_opts))
const md = new Remarkable('commonmark')
md.renderer.rules.link_open = function(tokens, idx, options /* env */) {
var title = tokens[idx].title ? (' title="' + Remarkable.utils.escapeHtml(Remarkable.utils.replaceEntities(tokens[idx].title)) + '"') : '';
var target = options.linkTarget ? (' target="' + options.linkTarget + '"') : '';
return '<a href="/articles' + Remarkable.utils.escapeHtml(tokens[idx].href) + '"' + title + target + '>';
};
const toc_md_text = toc_md.render(markdown);
console.log(md.render(toc_md_text.content));
return this.sanitized.bypassSecurityTrustHtml(md.render(toc_md_text.content));
}
}
它生成一个链接列表(这是一个缩短的列表):
<ul>
<li><a href="/articles#introduction">Introduction</a></li>
<li><a href="/articles#downloads">Downloads</a></li>
</uL>
但是,显示的每个链接都是“file:///”+href,这当然是行不通的。有什么方法可以修复 href 以使其正常工作或其他方式。
在我的控制器中,我有这个功能:
private async _show() {
const db = await this.databaseService.get();
const id = this.route.snapshot.paramMap.get('id').split('-').join(' ');
const article$ = db.article
.findOne(toTitleCase(id))
.$;
this.sub = article$.subscribe(async article => {
this.article = article;
const attachment = await this.article.getAttachment('index.md');
this.text = this.markdownPipe.transform(await attachment.getStringData());
this.intermoduleservice.toc = this.markdownTocPipe.transform(await attachment.getStringData());
this.zone.run(() => {});
});
}
InterModuleService 是一项全球服务,用于将 TOC 推送到我所在的侧导航菜单中。当我通过此服务将 TOC html 推送到 Side Nav 时,似乎没有在 HTML 上执行渲染更新。所以[routerLink] 绑定或 Angular 特定代码永远不会正确更新。
【问题讨论】:
-
对不起,我不熟悉电子,但是通过使用
href而不是routerLink属性,您没有使用角度路由过程,这将导致整个页面重新加载而不是内部路由。这是故意的吗? -
是的,试过了,但是当我将 html 直接插入页面时,链接无法链接。
标签: javascript angular markdown