Skip to content

Commit

Permalink
feat: allow to read initial markdown text from the document
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Nov 19, 2018
1 parent 36fef76 commit ea30d48
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
12 changes: 9 additions & 3 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,18 @@ const store = new Vuex.Store({
},

actions: {
async fetchFile({commit, getters, dispatch}, path) {
async fetchFile({commit, getters, dispatch}, {path, text}) {
commit('TOGGLE_SIDEBAR', false)
commit('SET_FETCHING', true)
const file = getFilenameByPath(getters.config.sourcePath, path)
let [text] = await Promise.all([
fetch(file).then(res => res.text()),

await Promise.all([
!text &&
fetch(file)
.then(res => res.text())
.then(res => {
text = res
}),
dispatch('fetchPrismLanguages')
])

Expand Down
17 changes: 9 additions & 8 deletions src/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,18 @@ export default {
Rightbar
},
created() {
this.fetchFile()
mounted() {
const initialTextNode = document.getElementById('docute-initial-text')
this.fetchFile({
path: this.$route.path,
text: initialTextNode && initialTextNode.textContent
})
},
beforeRouteUpdate(to, from, next) {
next()
if (to.path !== from.path) {
this.fetchFile(to.path)
this.fetchFile({path: to.path})
}
},
Expand Down Expand Up @@ -105,11 +109,8 @@ export default {
},
methods: {
async fetchFile(path) {
if (typeof path === 'undefined') {
path = this.$route.path
}
await this.$store.dispatch('fetchFile', path)
async fetchFile(opts) {
await this.$store.dispatch('fetchFile', opts)
hooks.invoke('onContentWillUpdate', this)
await this.$nextTick()
hooks.invoke('onContentUpdated', this)
Expand Down

0 comments on commit ea30d48

Please sign in to comment.