@@ -4,12 +4,13 @@ import matter from "gray-matter";
4
4
import { extractHeadingsFromMarkdown , slugify } from "utils" ;
5
5
import { Octokit } from "octokit" ;
6
6
import { throttling } from "@octokit/plugin-throttling" ;
7
- import { Lang , getHighlighter } from "shiki" ;
7
+ import { Lang } from "shiki" ;
8
8
import YAML from "yaml" ;
9
9
import MarkdownIt from "markdown-it" ;
10
10
import { cache } from "react" ;
11
11
import { current } from "consts" ;
12
12
import { Chapters } from "types" ;
13
+ import { getOrCreateHighlighter , highlightCode } from "utils/highlighter" ;
13
14
14
15
export const MyOctokit = Octokit . plugin ( throttling ) ;
15
16
const sidebarMemoryCache = new Map ( ) ;
@@ -144,6 +145,7 @@ const imgRegex = /<img([^>]*)src="([^"]+)"([^>]*)>/g;
144
145
const linkRegex = / < a ( [ ^ > ] * ) h r e f = " ( [ ^ " ] + ) " ( [ ^ > ] * ) > / g;
145
146
146
147
const headingRegex = / < h ( [ 2 - 4 ] ) > ( .* ?) < \/ h \1> / gm;
148
+ const codeBlockWithoutLangRegex = / (?: ^ | \n ) { 4 } ( \$ | # ) ( .* (?: \n { 4 } .+ ) * ) / g;
147
149
148
150
function getLang ( block : string ) : string {
149
151
const language = block . match ( codeLanguage ) ;
@@ -168,27 +170,12 @@ export const getHtmlFromGithubContent = async (
168
170
typographer : true ,
169
171
} ) ;
170
172
171
- const highlighter = await getHighlighter ( {
172
- themes : [ "github-light" , "one-dark-pro" ] ,
173
- } ) ;
174
- const languages = highlighter . getLoadedLanguages ( ) ;
175
-
176
- const highlightCode = ( code : string , language : string ) => {
177
- language = language . toLowerCase ( ) ;
178
- const langExists = languages . includes ( language as Lang ) ;
179
-
180
- const lang = langExists ? language : "shell" ;
181
-
182
- return (
183
- highlighter . codeToHtml ( code , { lang, theme : "one-dark-pro" } ) +
184
- highlighter . codeToHtml ( code , { lang, theme : "github-light" } )
185
- ) ;
186
- } ;
173
+ await getOrCreateHighlighter ( ) ;
187
174
188
175
md . options . highlight = highlightCode ;
189
176
190
177
// convert code selectors
191
- const transformCodeBlocks = ( markdown : string ) => {
178
+ const transformCodeSelectors = ( markdown : string ) => {
192
179
{
193
180
const matches = markdown . match ( codeInside ) ;
194
181
@@ -235,7 +222,10 @@ export const getHtmlFromGithubContent = async (
235
222
}
236
223
} ;
237
224
238
- const transformedMarkdown = transformCodeBlocks ( result ) ;
225
+ const transformedMarkdown = transformCodeSelectors ( result )
226
+ . replace ( codeBlockWithoutLangRegex , "\n```\n$1$2\n```\n" ) // convert codeblocks with indentation into classic codeblocks
227
+ . replace ( / ^ (?: { 4 } ) / gm, "" ) ;
228
+
239
229
return md
240
230
. render ( transformedMarkdown )
241
231
. replace (
0 commit comments