Skip to content

Commit 9a2c70c

Browse files
Sdjuzede
andauthored
fix: doesn't apply commented styles (#2321)
Co-authored-by: zede <[email protected]>
1 parent ceb5726 commit 9a2c70c

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

packages/slidev/node/syntax/transform/in-page-css.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import type { MarkdownTransformContext } from '@slidev/types'
2-
import { getCodeBlocks } from './utils'
2+
import { getCodeBlocks, getCommentBlocks } from './utils'
33

44
/**
55
* Transform <style> in markdown to scoped style with page selector
66
*/
77
export function transformPageCSS(ctx: MarkdownTransformContext) {
88
const codeBlocks = getCodeBlocks(ctx.s.original)
9+
const commentBlocks = getCommentBlocks(ctx.s.original)
910

1011
ctx.s.replace(
1112
/(\n<style[^>]*>)([\s\S]+?)(<\/style>)/g,
1213
(full, start, css, end, index) => {
1314
if (codeBlocks.isInsideCodeblocks(index))
1415
return full
16+
if (commentBlocks.isInsideCommentBlocks(index))
17+
return ``
1518
if (!start.includes('scoped'))
1619
start = start.replace('<style', '<style scoped')
1720
return `${start}\n${css}${end}`

packages/slidev/node/syntax/transform/slot-sugar.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import type { MarkdownTransformContext } from '@slidev/types'
2-
import { getCodeBlocks } from './utils'
2+
import { getCodeBlocks, getCommentBlocks } from './utils'
33

44
export function transformSlotSugar(
55
ctx: MarkdownTransformContext,
66
) {
77
const linesWithNewline = ctx.s.original.split(/(\r?\n)/g)
88
const codeBlocks = getCodeBlocks(ctx.s.original)
9+
const commentBlocks = getCommentBlocks(ctx.s.original)
910

1011
const lines: string[] = []
1112
for (let i = 0; i < linesWithNewline.length; i += 2) {
@@ -20,7 +21,7 @@ export function transformSlotSugar(
2021
lines.forEach((line) => {
2122
const start = offset
2223
offset += line.length
23-
if (codeBlocks.isInsideCodeblocks(offset))
24+
if (codeBlocks.isInsideCodeblocks(offset) || commentBlocks.isInsideCommentBlocks(offset))
2425
return
2526
const match = line.match(/^::\s*([\w.\-:]+)\s*::(\s*)$/)
2627
if (match) {

packages/slidev/node/syntax/transform/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,28 @@ export function getCodeBlocks(md: string) {
2525
}
2626
}
2727

28+
export function getCommentBlocks(md: string) {
29+
const commentBlocks = Array
30+
.from(md.matchAll(/<!--[\s\S]*?-->/g))
31+
.map((m) => {
32+
const start = m.index!
33+
const end = m.index! + m[0].length
34+
const startLine = md.slice(0, start).match(/\n/g)?.length || 0
35+
const endLine = md.slice(0, end).match(/\n/g)?.length || 0
36+
return [start, end, startLine, endLine]
37+
})
38+
39+
return {
40+
commentBlocks,
41+
isInsideCommentBlocks(idx: number) {
42+
return commentBlocks.some(([s, e]) => s <= idx && idx <= e)
43+
},
44+
isLineInsideCommentBlocks(line: number) {
45+
return commentBlocks.some(([, , s, e]) => s <= line && line <= e)
46+
},
47+
}
48+
}
49+
2850
/**
2951
* Escape `{{` in code block to prevent Vue interpret it, #99, #1316
3052
*/

0 commit comments

Comments
 (0)