File tree Expand file tree Collapse file tree 3 files changed +54
-15
lines changed Expand file tree Collapse file tree 3 files changed +54
-15
lines changed Original file line number Diff line number Diff line change @@ -184,6 +184,13 @@ export const useStore = defineStore(`store`, () => {
184
184
185
185
const readingTime = ref < ReadTimeResults | null > ( null )
186
186
187
+ // 文章标题
188
+ const titleList = ref < {
189
+ url : string
190
+ title : string
191
+ level : number
192
+ } [ ] > ( [ ] )
193
+
187
194
// 更新编辑器
188
195
const editorRefresh = ( ) => {
189
196
codeThemeChange ( )
@@ -192,6 +199,26 @@ export const useStore = defineStore(`store`, () => {
192
199
const { markdownContent, readingTime : readingTimeResult } = renderer . parseFrontMatterAndContent ( editor . value ! . getValue ( ) )
193
200
readingTime . value = readingTimeResult
194
201
let outputTemp = marked . parse ( markdownContent ) as string
202
+
203
+ // 提取标题
204
+ const div = document . createElement ( 'div' )
205
+ div . innerHTML = outputTemp
206
+ const list = div . querySelectorAll < HTMLElement > ( `[data-heading]` )
207
+
208
+ titleList . value = [ ]
209
+ let i = 0
210
+ for ( const item of list ) {
211
+ item . setAttribute ( `id` , `${ i } ` )
212
+ titleList . value . push ( {
213
+ url : `#${ i } ` ,
214
+ title : `${ item . innerText } ` ,
215
+ level : Number ( item . tagName . slice ( 1 ) )
216
+ } )
217
+ i ++
218
+ }
219
+
220
+ outputTemp = div . innerHTML
221
+
195
222
outputTemp = DOMPurify . sanitize ( outputTemp )
196
223
197
224
// 阅读时间及字数统计
@@ -501,6 +528,8 @@ export const useStore = defineStore(`store`, () => {
501
528
delPost,
502
529
isOpenPostSlider,
503
530
isOpenRightSlider,
531
+
532
+ titleList,
504
533
}
505
534
} )
506
535
Original file line number Diff line number Diff line change @@ -157,7 +157,8 @@ export function initRenderer(opts: IOpts) {
157
157
158
158
function styledContent ( styleLabel : string , content : string , tagName ?: string ) : string {
159
159
const tag = tagName ?? styleLabel
160
- return `<${ tag } ${ styles ( styleLabel ) } >${ content } </${ tag } >`
160
+
161
+ return `<${ tag } ${ / ^ h \d $ / . test ( tag ) ? `data-heading="true"` : `` } ${ styles ( styleLabel ) } >${ content } </${ tag } >`
161
162
}
162
163
163
164
function addFootnote ( title : string , link : string ) : number {
Original file line number Diff line number Diff line change @@ -418,24 +418,33 @@ onMounted(() => {
418
418
</ContextMenuContent >
419
419
</ContextMenu >
420
420
</div >
421
- <div
422
- id =" preview"
423
- ref =" preview"
424
- class =" preview-wrapper flex-1 p-5"
425
- >
426
- <div id =" output-wrapper" :class =" { output_night: !backLight }" >
427
- <div class =" preview border-x-1 shadow-xl" >
428
- <section id =" output" v-html =" output" />
429
- <div v-if =" isCoping" class =" loading-mask" >
430
- <div class =" loading-mask-box" >
431
- <div class =" loading__img" />
432
- <span >正在生成</span >
421
+ <div class =" relative flex-1" >
422
+ <div
423
+ id =" preview"
424
+ ref =" preview"
425
+ class =" preview-wrapper p-5"
426
+ >
427
+ <div id =" output-wrapper" :class =" { output_night: !backLight }" >
428
+ <div class =" preview border-x-1 shadow-xl" >
429
+ <section id =" output" v-html =" output" />
430
+ <div v-if =" isCoping" class =" loading-mask" >
431
+ <div class =" loading-mask-box" >
432
+ <div class =" loading__img" />
433
+ <span >正在生成</span >
434
+ </div >
433
435
</div >
434
436
</div >
435
437
</div >
436
- </div >
437
438
438
- <BackTop target =" preview" :right =" 40" :bottom =" 40" />
439
+ <BackTop target =" preview" :right =" 40" :bottom =" 40" />
440
+ </div >
441
+ <ul class =" bg-background absolute left-0 top-0 max-h-100 w-60 overflow-auto border rounded-2 p-2 text-sm shadow" >
442
+ <li v-for =" (item, index) in store.titleList" :key =" index" class =" line-clamp-1 py-1 leading-6 hover:bg-gray-300 dark:hover:bg-gray-600" :style =" {paddingLeft: item.level - 0.5 + 'em'}" >
443
+ <a :href =" item.url" >
444
+ {{ item.title }}
445
+ </a >
446
+ </li >
447
+ </ul >
439
448
</div >
440
449
<CssEditor class =" order-2 flex-1" />
441
450
<RightSlider class =" order-2" />
You can’t perform that action at this time.
0 commit comments