Skip to content

Commit 4b53c0d

Browse files
authored
Fix Katex parsed HTML (#187)
1 parent 6ba4c2a commit 4b53c0d

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

angular.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"node_modules/prismjs/components/prism-c.min.js",
4545
"node_modules/prismjs/components/prism-cpp.min.js",
4646
"node_modules/prismjs/components/prism-javascript.min.js",
47+
"node_modules/prismjs/components/prism-latex.min.js",
4748
"node_modules/prismjs/components/prism-markdown.min.js",
4849
"node_modules/prismjs/components/prism-python.min.js",
4950
"node_modules/prismjs/components/prism-typescript.min.js",

demo/src/app/app.component.html

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,19 +331,18 @@ <h2 class="subtitle">KaTeX plugin</h2>
331331
### Example
332332
</markdown>
333333

334-
<markdown ngPreserveWhitespaces>
334+
<markdown>
335335
You can render KaTex expression by adding `katex` directive on the `markdown` component/directive.
336336
</markdown>
337337

338338
<markdown [src]="'app/remote/katex.html'"></markdown>
339339

340-
<markdown ngPreserveWhitespaces>
340+
<markdown>
341341
The example below illustrate `katex` directive in action.
342342
</markdown>
343343

344-
<markdown katex>
345-
$f(x) = \int_&#123;-\infty}^\infty \hat f(\xi) e^&#123;2 \pi i \xi x} d\xi$
346-
</markdown>
344+
<textarea class="katex-textarea" [(ngModel)]="katexMarkdown"></textarea>
345+
<markdown class="katex-usage" [data]="katexMarkdown" katex></markdown>
347346

348347
<markdown ngPreserveWhitespaces>
349348
Optionally, you can specify math rendering [options](https://katex.org/docs/options.html) using `katexOptions` property.

demo/src/app/app.component.scss

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ section:first-of-type {
130130
padding: 13px;
131131
}
132132

133+
.katex-usage,
134+
.katex-textarea,
133135
.pipe-usage,
134136
.pipe-textarea,
135137
.variable-binding,
@@ -141,14 +143,24 @@ section:first-of-type {
141143
}
142144
}
143145

146+
.katex-textarea {
147+
min-height: 220px;
148+
}
149+
150+
.pipe-textarea,
151+
.variable-textarea {
152+
min-height: 320px;
153+
}
154+
155+
.katex-textarea,
144156
.pipe-textarea,
145157
.variable-textarea {
146158
border-radius: 4px;
147159
margin-top: 8px;
148-
min-height: 320px;
149160
padding: 8px;
150161
}
151162

163+
.katex-usage,
152164
.pipe-usage,
153165
.variable-binding {
154166
display: block;

demo/src/app/app.component.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ export class MarkdownDemoComponent {
6161
}`;
6262
//#endregion
6363

64+
//#region katex
65+
katexMarkdown =
66+
`#### \`katex\` directive example
67+
68+
\`\`\`latex
69+
f(x) = \\int_{-\\infty}^\\infty \\hat f(\\xi) e^{2 \\pi i \\xi x} d\\xi
70+
\`\`\`
71+
72+
$f(x) = \\int_{-\\infty}^\\infty \\hat f(\\xi) e^{2 \\pi i \\xi x} d\\xi$
73+
74+
> If you can understand this, you're a genius!`;
75+
//#endregion
76+
6477
protected _titleIsAnimating = false;
6578
protected _pushpinIsOn = false;
6679

lib/src/markdown.service.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ declare var Prism: {
1818
highlightAllUnder: (element: Element | Document) => void;
1919
};
2020

21-
// tslint:disable-next-line:max-line-length
21+
// tslint:disable:max-line-length
22+
export const errorKatexNotLoaded = '[ngx-markdown When using the [katex] attribute you *have to* include KaTeX files to `angular.json` or use imports. See README for more information';
2223
export const errorSrcWithoutHttpClient = '[ngx-markdown] When using the [src] attribute you *have to* pass the `HttpClient` as a parameter of the `forRoot` method. See README for more information';
23-
// tslint:disable-next-line:max-line-length
24-
export const errorKatexNotLoaded = '[ngx-markdown] When using the [katex] attribute you *have to* include KaTeX files to `angular.json` or use imports. See README for more information';
24+
// tslint:enable:max-line-length
2525

2626
@Injectable()
2727
export class MarkdownService {
28-
private _options: MarkedOptions;
2928

29+
private _options: MarkedOptions;
3030
get options(): MarkedOptions { return this._options; }
3131
set options(value: MarkedOptions) {
3232
this._options = Object.assign({},
@@ -79,11 +79,11 @@ export class MarkdownService {
7979
}
8080
}
8181

82-
renderKatex(markdown: string, options?: KatexOptions): string {
82+
renderKatex(html: string, options?: KatexOptions): string {
8383
if (typeof katex === 'undefined' || typeof katex.renderToString === 'undefined') {
8484
throw new Error(errorKatexNotLoaded);
8585
}
86-
return markdown.replace(/(?:^|[^\\])\$([^\s][^$]*?[^\s])\$/gm, (_, tex) => katex.renderToString(tex, options));
86+
return html.replace(/\$([^\s][^$]*?[^\s])\$/gm, (_, tex) => katex.renderToString(tex, options));
8787
}
8888

8989
private decodeHtml(html: string): string {

0 commit comments

Comments
 (0)