-
-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(content): fix deprecation warnings for marked package (#487)
- Loading branch information
1 parent
a49376c
commit 87a978b
Showing
6 changed files
with
135 additions
and
68 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
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
75 changes: 9 additions & 66 deletions
75
packages/content/src/lib/markdown-content-renderer.service.ts
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 |
---|---|---|
@@ -1,86 +1,29 @@ | ||
/** | ||
* Credit goes to Scully for original implementation | ||
* https://github.com/scullyio/scully/blob/main/libs/scully/src/lib/fileHanderPlugins/markdown.ts | ||
*/ | ||
import { inject, Injectable, PLATFORM_ID, Provider } from '@angular/core'; | ||
import { marked } from 'marked'; | ||
|
||
import 'prismjs'; | ||
import 'prismjs/plugins/toolbar/prism-toolbar'; | ||
import 'prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard'; | ||
import 'prismjs/components/prism-bash'; | ||
import 'prismjs/components/prism-css'; | ||
import 'prismjs/components/prism-javascript'; | ||
import 'prismjs/components/prism-json'; | ||
import 'prismjs/components/prism-markup'; | ||
import 'prismjs/components/prism-typescript'; | ||
|
||
import { ContentRenderer } from './content-renderer'; | ||
|
||
declare const Prism: typeof import('prismjs'); | ||
|
||
const renderer = new marked.Renderer(); | ||
// wrap code block the way Prism.js expects it | ||
renderer.code = function (this: any, code, lang) { | ||
// eslint-disable-next-line | ||
code = this.options.highlight(code, lang); | ||
if (!lang) { | ||
return '<pre><code>' + code + '</code></pre>'; | ||
} | ||
// e.g. "language-js" | ||
const langClass = 'language-' + lang; | ||
return ( | ||
'<pre class="' + | ||
langClass + | ||
'"><code class="' + | ||
langClass + | ||
'">' + | ||
code + | ||
'</code></pre>' | ||
); | ||
}; | ||
// ------------------------------ | ||
import { MarkedSetupService } from './marked-setup.service'; | ||
|
||
@Injectable() | ||
export class MarkdownContentRendererService implements ContentRenderer { | ||
platformId = inject(PLATFORM_ID); | ||
#marked = inject(MarkedSetupService, { self: true }); | ||
|
||
async render(content: string) { | ||
marked.setOptions({ | ||
renderer, | ||
highlight: (code, lang) => { | ||
lang = lang || 'typescript'; | ||
if (!Prism.languages[lang]) { | ||
console.warn(`Notice: | ||
--------------------------------------------------------------------------------------- | ||
The requested language '${lang}' is not available with the provided setup. | ||
To enable, import your main.ts as: | ||
import 'prismjs/components/prism-${lang}'; | ||
--------------------------------------------------------------------------------------- | ||
`); | ||
return code; | ||
} | ||
return Prism.highlight(code, Prism.languages[lang], lang); | ||
}, | ||
pedantic: false, | ||
gfm: true, | ||
breaks: false, | ||
sanitize: false, | ||
smartypants: false, | ||
xhtml: false, | ||
}); | ||
|
||
return marked(content); | ||
return this.#marked.getMarkedInstance().parse(content); | ||
} | ||
|
||
// eslint-disable-next-line | ||
enhance() {} | ||
} | ||
|
||
export function withMarkdownRenderer(): Provider { | ||
return { provide: ContentRenderer, useClass: MarkdownContentRendererService }; | ||
return { | ||
provide: ContentRenderer, | ||
useClass: MarkdownContentRendererService, | ||
deps: [MarkedSetupService], | ||
}; | ||
} | ||
|
||
export function provideContent(...features: Provider[]) { | ||
return [...features]; | ||
return [...features, MarkedSetupService]; | ||
} |
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,80 @@ | ||
/** | ||
* Credit goes to Scully for original implementation | ||
* https://github.com/scullyio/scully/blob/main/libs/scully/src/lib/fileHanderPlugins/markdown.ts | ||
*/ | ||
import { Injectable } from '@angular/core'; | ||
import { marked } from 'marked'; | ||
import { gfmHeadingId } from 'marked-gfm-heading-id'; | ||
import { markedHighlight } from 'marked-highlight'; | ||
|
||
import 'prismjs'; | ||
import 'prismjs/plugins/toolbar/prism-toolbar'; | ||
import 'prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard'; | ||
import 'prismjs/components/prism-bash'; | ||
import 'prismjs/components/prism-css'; | ||
import 'prismjs/components/prism-javascript'; | ||
import 'prismjs/components/prism-json'; | ||
import 'prismjs/components/prism-markup'; | ||
import 'prismjs/components/prism-typescript'; | ||
|
||
declare const Prism: typeof import('prismjs'); | ||
|
||
@Injectable() | ||
export class MarkedSetupService { | ||
private readonly marked: typeof marked; | ||
|
||
constructor() { | ||
const renderer = new marked.Renderer(); | ||
renderer.code = (code, lang) => { | ||
if (!lang) { | ||
return '<pre><code>' + code + '</code></pre>'; | ||
} | ||
const langClass = 'language-' + lang; | ||
const html = | ||
'<pre class="' + | ||
langClass + | ||
'"><code class="' + | ||
langClass + | ||
'">' + | ||
code + | ||
'</code></pre>'; | ||
return html; | ||
}; | ||
|
||
marked.use( | ||
gfmHeadingId(), | ||
markedHighlight({ | ||
highlight: (code, lang) => { | ||
lang = lang || 'typescript'; | ||
if (!Prism.languages[lang]) { | ||
console.warn(`Notice: | ||
--------------------------------------------------------------------------------------- | ||
The requested language '${lang}' is not available with the provided setup. | ||
To enable, import your main.ts as: | ||
import 'prismjs/components/prism-${lang}'; | ||
--------------------------------------------------------------------------------------- | ||
`); | ||
return code; | ||
} | ||
return Prism.highlight(code, Prism.languages[lang], lang); | ||
}, | ||
}), | ||
{ | ||
renderer, | ||
pedantic: false, | ||
gfm: true, | ||
breaks: false, | ||
sanitize: false, | ||
smartypants: false, | ||
xhtml: false, | ||
mangle: false, | ||
} | ||
); | ||
|
||
this.marked = marked; | ||
} | ||
|
||
getMarkedInstance(): typeof marked { | ||
return this.marked; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.