-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(embed): render html content for snippet not found
- Loading branch information
1 parent
403a6dd
commit 381ea53
Showing
10 changed files
with
98 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "sharingan-embed", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"repository": "https://github.com/tericcabrel/sharingan.git", | ||
"author": "Eric Cabrel TIOGO <[email protected]>", | ||
"license": "MIT" | ||
|
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,41 @@ | ||
import { Snippet } from '@sharingan/database'; | ||
import { Lang } from 'shiki'; | ||
|
||
import { Shiki } from '../types'; | ||
import { addWhitespaceForEmptyLine, generateLineHighlightOptions } from './utils'; | ||
|
||
export const generateNoSnippetHtmlContent = (webAppUrl: string) => { | ||
return `<div class="no-content"> | ||
<h3>Oops! Snippet not found!</h3> | ||
<div>Go to <a href="${webAppUrl}" target="_blank">Sharingan</a> to ensure it exists and is accessible</div> | ||
</div>`; | ||
}; | ||
|
||
export const generateSnippetHtmlContent = async ({ shiki, snippet }: { shiki: Shiki; snippet: Snippet }) => { | ||
const highlighter = await shiki.getHighlighter({ | ||
langs: [snippet.language] as Lang[], | ||
theme: snippet.theme, | ||
themes: [snippet.theme], | ||
}); | ||
|
||
const snippetCodeHtml = highlighter.codeToHtml(snippet.content.trim(), { | ||
lang: snippet.language, | ||
lineOptions: generateLineHighlightOptions(snippet.lineHighlight), | ||
}); | ||
|
||
const backgroundColor = highlighter.getBackgroundColor(); | ||
|
||
const html = snippetCodeHtml | ||
.replace(/<pre class="shiki" style="background-color: \#[\w]{6}">/, '') | ||
.replace('</pre>', '') | ||
.split('\n') | ||
.map((line: string, i: number) => { | ||
return `<span class='line-number'>${i + 1}</span>${addWhitespaceForEmptyLine(line)}`; | ||
}) | ||
.join('\n'); | ||
|
||
return { | ||
backgroundColor, | ||
html, | ||
}; | ||
}; |
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
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,5 @@ | ||
import { Highlighter, HighlighterOptions } from 'shiki'; | ||
|
||
export type Shiki = { | ||
getHighlighter: (options: HighlighterOptions) => Promise<Highlighter>; | ||
}; |
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