-
Notifications
You must be signed in to change notification settings - Fork 4.7k
markdown: <TOC/> component, fix #1275 [WIP] #1307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f4185c3
remove colon as separator
shigma fb778e4
Merge branch 'master' of https://github.com/vuejs/vuepress
shigma ea6e17f
Merge remote-tracking branch 'upstream/master'
shigma ce5257d
fix #1227: relative path in pages that use permalink
shigma 91a0b75
toc component
shigma 73f2ff8
support listType, includeLevel, containerHeaderHtml and containerFoot…
shigma ba03c2e
Merge remote-tracking branch 'upstream/master' into toc-component
shigma 7549230
using slots instead of v-html
shigma d7ce374
update docs
shigma c3ed29a
move toc component into core/lib/app/components
shigma ef42be4
Merge branch 'master' into toc-component
shigma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,23 @@ | ||
<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 hidden or 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,65 @@ | ||
<template> | ||
<div> | ||
<p class="header" v-html="containerHeaderHtml" /> | ||
<HeaderList :items="groupedHeaders" :list-type="listTypes" /> | ||
<p class="footer" v-html="containerFooterHtml" /> | ||
shigma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</div> | ||
</template> | ||
|
||
<script> | ||
|
||
import HeaderList from './HeaderList.vue' | ||
|
||
export default { | ||
props: { | ||
listType: { | ||
type: [String, Array], | ||
default: 'ul' | ||
}, | ||
includeLevel: { | ||
type: Array, | ||
default: () => [2, 3] | ||
}, | ||
containerHeaderHtml: String, | ||
containerFooterHtml: String | ||
}, | ||
|
||
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 hidden or 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 hidden or 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 hidden or 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,80 @@ | ||
// 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} | ||
:container-header-html=${options.containerHeaderHtml} | ||
:container-footer-html=${options.containerFooterHtml} | ||
>` | ||
} | ||
|
||
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.