-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
12 changed files
with
244 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<template> | ||
<component :is="listType[0]"> | ||
<li v-for="(item, index) in items" :key="index"> | ||
<router-link :to="'#' + item.slug" v-text="item.title" /> | ||
<HeaderList v-if="item.children" :items="item.children" :list-type="innerListType" /> | ||
</li> | ||
</component> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
name: 'HeaderList', | ||
props: ['items', 'listType'], | ||
computed: { | ||
innerListType () { | ||
return this.listType.slice(Math.min(this.listType.length - 1, 1)) | ||
} | ||
} | ||
} | ||
</script> |
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,57 @@ | ||
<template> | ||
<div> | ||
<slot name="header" /> | ||
<HeaderList :items="groupedHeaders" :list-type="listTypes" /> | ||
<slot name="footer" /> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import HeaderList from './HeaderList.vue' | ||
export default { | ||
props: { | ||
listType: { | ||
type: [String, Array], | ||
default: 'ul' | ||
}, | ||
includeLevel: { | ||
type: Array, | ||
default: () => [2, 3] | ||
} | ||
}, | ||
components: { HeaderList }, | ||
computed: { | ||
listTypes () { | ||
return typeof this.listType === 'string' ? [this.listType] : this.listType | ||
}, | ||
groupedHeaders () { | ||
return this.groupHeaders(this.$page.headers).list | ||
} | ||
}, | ||
methods: { | ||
groupHeaders (headers, startLevel = 1) { | ||
const list = [] | ||
let index = 0 | ||
while (index < headers.length) { | ||
const header = headers[index] | ||
if (header.level < startLevel) break | ||
if (header.level > startLevel) { | ||
const result = this.groupHeaders(headers.slice(index), header.level) | ||
if (list.length) { | ||
list[list.length - 1].children = result.list | ||
} else { | ||
list.push(...result.list) | ||
} | ||
index += result.index | ||
} else { | ||
if (header.level <= this.includeLevel[1] && header.level >= this.includeLevel[0]) { | ||
list.push({ ...header }) | ||
} | ||
index += 1 | ||
} | ||
} | ||
return { list, index } | ||
} | ||
} | ||
} | ||
</script> |
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,83 @@ | ||
// reference: https://github.com/Oktavilla/markdown-it-table-of-contents | ||
|
||
const defaults = { | ||
includeLevel: [2, 3], | ||
containerClass: 'table-of-contents', | ||
markerPattern: /^\[\[toc\]\]/im, | ||
listType: 'ul', | ||
containerHeaderHtml: '', | ||
containerFooterHtml: '' | ||
} | ||
|
||
module.exports = (md, options) => { | ||
options = Object.assign({}, defaults, options) | ||
const tocRegexp = options.markerPattern | ||
|
||
function toc (state, silent) { | ||
var token | ||
var match | ||
|
||
// Reject if the token does not start with [ | ||
if (state.src.charCodeAt(state.pos) !== 0x5B /* [ */) { | ||
return false | ||
} | ||
// Don't run any pairs in validation mode | ||
if (silent) { | ||
return false | ||
} | ||
|
||
// Detect TOC markdown | ||
match = tocRegexp.exec(state.src) | ||
match = !match ? [] : match.filter(function (m) { return m }) | ||
if (match.length < 1) { | ||
return false | ||
} | ||
|
||
// Build content | ||
token = state.push('toc_open', 'toc', 1) | ||
token.markup = '[[toc]]' | ||
token = state.push('toc_body', '', 0) | ||
token = state.push('toc_close', 'toc', -1) | ||
|
||
// Update pos so the parser can continue | ||
var newline = state.src.indexOf('\n') | ||
if (newline !== -1) { | ||
state.pos = state.pos + newline | ||
} else { | ||
state.pos = state.pos + state.posMax + 1 | ||
} | ||
|
||
return true | ||
} | ||
|
||
md.renderer.rules.toc_open = function () { | ||
return vBindEscape`<TOC | ||
:class=${options.containerClass} | ||
:list-type=${options.listType} | ||
:include-level=${options.includeLevel} | ||
>` | ||
} | ||
|
||
md.renderer.rules.toc_body = function () { | ||
return `<template slot="header">${options.containerHeaderHtml}</template>` | ||
+ `<template slot="footer">${options.containerFooterHtml}</template>` | ||
} | ||
|
||
md.renderer.rules.toc_close = function () { | ||
return `</TOC>` | ||
} | ||
|
||
// Insert TOC | ||
md.inline.ruler.after('emphasis', 'toc', toc) | ||
} | ||
|
||
/** escape double quotes in v-bind derivatives */ | ||
function vBindEscape (strs, ...args) { | ||
return strs.reduce((prev, curr, index) => { | ||
return prev + curr + (index >= args.length | ||
? '' | ||
: `"${JSON.stringify(args[index]) | ||
.replace(/"/g, "'") | ||
.replace(/([^\\])(\\\\)*\\'/g, (_, char) => char + '\\u0022')}"`) | ||
}, '') | ||
} |
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
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