<template lang="pug">
  vue-markdown(:source="data")
</template>

<script>
import VueMarkdown from "@/components/VueMarkdown"
export default {
  name: "Document",
  components: {VueMarkdown},
  data() {
    return {
      data: ''
    }
  },
  created() {
    // This ensures to not update twice after editing
    setTimeout(() => {
      this.$ws.addCMDListener(6, this.getFile)
    }, 1000)
  },
  beforeDestroy() {
    this.$ws.removeCMDListener(6, this.getFile)
  },
  methods: {
    getFile(update) {
      if(update !== this.filename) return
      fetch("/_doc/" + this.filename)
          .then(response => response.text())
          .then(text => this.data = text)
    }
  },
  computed: {
    filename() {
      return this.$route.params.pathMatch
    }
  },
  mounted() {
    this.getFile(this.filename)
  }
}
</script>

<style scoped>

</style>