retrieve content from markdown file and convert it to valid HTML code in vuejs












0















I want to create a documentation page and have some markdown files which represent the main content. I have a navigation sidebar where I can select the specific content.



When clicking on a navigation item I need to read the content from a markdown file. I have a method that returns me the required path but I don't know how to read the file.



Lastly I took marked to render the markdown syntax to HTML code.



I created a small example that shows what is missing



https://codesandbox.io/s/006p3m1p1l



Is there something I can use to read the markdown content?










share|improve this question





























    0















    I want to create a documentation page and have some markdown files which represent the main content. I have a navigation sidebar where I can select the specific content.



    When clicking on a navigation item I need to read the content from a markdown file. I have a method that returns me the required path but I don't know how to read the file.



    Lastly I took marked to render the markdown syntax to HTML code.



    I created a small example that shows what is missing



    https://codesandbox.io/s/006p3m1p1l



    Is there something I can use to read the markdown content?










    share|improve this question



























      0












      0








      0








      I want to create a documentation page and have some markdown files which represent the main content. I have a navigation sidebar where I can select the specific content.



      When clicking on a navigation item I need to read the content from a markdown file. I have a method that returns me the required path but I don't know how to read the file.



      Lastly I took marked to render the markdown syntax to HTML code.



      I created a small example that shows what is missing



      https://codesandbox.io/s/006p3m1p1l



      Is there something I can use to read the markdown content?










      share|improve this question
















      I want to create a documentation page and have some markdown files which represent the main content. I have a navigation sidebar where I can select the specific content.



      When clicking on a navigation item I need to read the content from a markdown file. I have a method that returns me the required path but I don't know how to read the file.



      Lastly I took marked to render the markdown syntax to HTML code.



      I created a small example that shows what is missing



      https://codesandbox.io/s/006p3m1p1l



      Is there something I can use to read the markdown content?







      vue.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 20:09









      niklaz

      1,99511217




      1,99511217










      asked Jan 2 at 16:34









      Question3rQuestion3r

      625328




      625328
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Use VueResource to retrieve the content from your markdown file.
          Import the VueResource, and add it using Vue.use method (main.js):



          import Vue from "vue";
          import App from "./App";
          import VueResource from "vue-resource";

          Vue.config.productionTip = false;
          Vue.use(VueResource);

          new Vue({
          el: "#app",
          components: { App },
          template: "<App/>"
          });


          Then use this.$http.get() method it within your App.vue file to retrieve the markdown file conent.
          You can use markdown parsing library, like Showdown.js, wrapped within a vue.js method, directive or filter.



          See: https://github.com/showdownjs/showdown and http://showdownjs.com/



          There is also vuejs component wrapper for Showdown:
          See: https://github.com/meteorlxy/vue-showdown and https://vue-showdown.js.org/



          In your case that should look something like this ( using vue-showdown):



          <template>
          <div id="app"><VueShowdown :markdown="fileContent"></VueShowdown></div>
          </template>

          <script>
          import VueShowdown from "vue-showdown";

          export default {
          name: "App",
          components: VueShowdown,
          data: function() {
          return {
          fileContent: null,
          fileToRender:
          "https://gist.githubusercontent.com/rt2zz/e0a1d6ab2682d2c47746950b84c0b6ee/raw/83b8b4814c3417111b9b9bef86a552608506603e/markdown-sample.md",
          rawContent: null
          };
          },
          created: function() {
          // const fileToRender = `./assets/documentation/general/welcome.md`;
          //const rawContent = ""; // Read the file content using fileToRender
          // this.fileContent = "### marked(rawContent) should get executed";
          this.getContent();
          },
          methods: {
          getContent() {
          this.fileContent = "rendering ";
          // var self;
          this.$http.get(this.fileToRender).then(
          response => {
          // get body data

          this.fileContent = response.body;
          },
          response => {
          // error callback
          this.fileContent = "An error ocurred";
          }
          );
          }
          }
          };
          </script>


          Check in sandbox: https://codesandbox.io/s/poknq9z6q



          If your markdown file load is one time thing, then you could import it data, just like you import the components, js files and libraries:



          <template>
          <div id="app"><VueShowdown :markdown="fileContent"></VueShowdown></div>
          </template>

          <script>
          import VueShowdown from "vue-showdown";

          import MarkDownData from './assets/documentation/general/welcome.md';

          export default {
          name: "App",
          components: VueShowdown,
          data: function() {
          return {
          fileContent: null,
          rawContent: null
          };
          },
          created: function() {
          // const fileToRender = `./assets/documentation/general/welcome.md`;
          //const rawContent = ""; // Read the file content using fileToRender
          // this.fileContent = "### marked(rawContent) should get executed";
          this.getContent();
          },
          methods: {
          getContent() {
          this.fileContent = MarkDownData;
          }
          }
          };
          </script>


          See: https://codesandbox.io/s/xpmy7pzyqz






          share|improve this answer


























          • I upvoted it because it works but I don't know if this is what I'm looking for. Your first sandbox: I want to read a local file without using a http request. Your second sandbox: Sorry, I need to read it on the fly. So the import would happen within the getContent() function multiple times...

            – Question3r
            Jan 2 at 20:22











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009901%2fretrieve-content-from-markdown-file-and-convert-it-to-valid-html-code-in-vuejs%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Use VueResource to retrieve the content from your markdown file.
          Import the VueResource, and add it using Vue.use method (main.js):



          import Vue from "vue";
          import App from "./App";
          import VueResource from "vue-resource";

          Vue.config.productionTip = false;
          Vue.use(VueResource);

          new Vue({
          el: "#app",
          components: { App },
          template: "<App/>"
          });


          Then use this.$http.get() method it within your App.vue file to retrieve the markdown file conent.
          You can use markdown parsing library, like Showdown.js, wrapped within a vue.js method, directive or filter.



          See: https://github.com/showdownjs/showdown and http://showdownjs.com/



          There is also vuejs component wrapper for Showdown:
          See: https://github.com/meteorlxy/vue-showdown and https://vue-showdown.js.org/



          In your case that should look something like this ( using vue-showdown):



          <template>
          <div id="app"><VueShowdown :markdown="fileContent"></VueShowdown></div>
          </template>

          <script>
          import VueShowdown from "vue-showdown";

          export default {
          name: "App",
          components: VueShowdown,
          data: function() {
          return {
          fileContent: null,
          fileToRender:
          "https://gist.githubusercontent.com/rt2zz/e0a1d6ab2682d2c47746950b84c0b6ee/raw/83b8b4814c3417111b9b9bef86a552608506603e/markdown-sample.md",
          rawContent: null
          };
          },
          created: function() {
          // const fileToRender = `./assets/documentation/general/welcome.md`;
          //const rawContent = ""; // Read the file content using fileToRender
          // this.fileContent = "### marked(rawContent) should get executed";
          this.getContent();
          },
          methods: {
          getContent() {
          this.fileContent = "rendering ";
          // var self;
          this.$http.get(this.fileToRender).then(
          response => {
          // get body data

          this.fileContent = response.body;
          },
          response => {
          // error callback
          this.fileContent = "An error ocurred";
          }
          );
          }
          }
          };
          </script>


          Check in sandbox: https://codesandbox.io/s/poknq9z6q



          If your markdown file load is one time thing, then you could import it data, just like you import the components, js files and libraries:



          <template>
          <div id="app"><VueShowdown :markdown="fileContent"></VueShowdown></div>
          </template>

          <script>
          import VueShowdown from "vue-showdown";

          import MarkDownData from './assets/documentation/general/welcome.md';

          export default {
          name: "App",
          components: VueShowdown,
          data: function() {
          return {
          fileContent: null,
          rawContent: null
          };
          },
          created: function() {
          // const fileToRender = `./assets/documentation/general/welcome.md`;
          //const rawContent = ""; // Read the file content using fileToRender
          // this.fileContent = "### marked(rawContent) should get executed";
          this.getContent();
          },
          methods: {
          getContent() {
          this.fileContent = MarkDownData;
          }
          }
          };
          </script>


          See: https://codesandbox.io/s/xpmy7pzyqz






          share|improve this answer


























          • I upvoted it because it works but I don't know if this is what I'm looking for. Your first sandbox: I want to read a local file without using a http request. Your second sandbox: Sorry, I need to read it on the fly. So the import would happen within the getContent() function multiple times...

            – Question3r
            Jan 2 at 20:22
















          1














          Use VueResource to retrieve the content from your markdown file.
          Import the VueResource, and add it using Vue.use method (main.js):



          import Vue from "vue";
          import App from "./App";
          import VueResource from "vue-resource";

          Vue.config.productionTip = false;
          Vue.use(VueResource);

          new Vue({
          el: "#app",
          components: { App },
          template: "<App/>"
          });


          Then use this.$http.get() method it within your App.vue file to retrieve the markdown file conent.
          You can use markdown parsing library, like Showdown.js, wrapped within a vue.js method, directive or filter.



          See: https://github.com/showdownjs/showdown and http://showdownjs.com/



          There is also vuejs component wrapper for Showdown:
          See: https://github.com/meteorlxy/vue-showdown and https://vue-showdown.js.org/



          In your case that should look something like this ( using vue-showdown):



          <template>
          <div id="app"><VueShowdown :markdown="fileContent"></VueShowdown></div>
          </template>

          <script>
          import VueShowdown from "vue-showdown";

          export default {
          name: "App",
          components: VueShowdown,
          data: function() {
          return {
          fileContent: null,
          fileToRender:
          "https://gist.githubusercontent.com/rt2zz/e0a1d6ab2682d2c47746950b84c0b6ee/raw/83b8b4814c3417111b9b9bef86a552608506603e/markdown-sample.md",
          rawContent: null
          };
          },
          created: function() {
          // const fileToRender = `./assets/documentation/general/welcome.md`;
          //const rawContent = ""; // Read the file content using fileToRender
          // this.fileContent = "### marked(rawContent) should get executed";
          this.getContent();
          },
          methods: {
          getContent() {
          this.fileContent = "rendering ";
          // var self;
          this.$http.get(this.fileToRender).then(
          response => {
          // get body data

          this.fileContent = response.body;
          },
          response => {
          // error callback
          this.fileContent = "An error ocurred";
          }
          );
          }
          }
          };
          </script>


          Check in sandbox: https://codesandbox.io/s/poknq9z6q



          If your markdown file load is one time thing, then you could import it data, just like you import the components, js files and libraries:



          <template>
          <div id="app"><VueShowdown :markdown="fileContent"></VueShowdown></div>
          </template>

          <script>
          import VueShowdown from "vue-showdown";

          import MarkDownData from './assets/documentation/general/welcome.md';

          export default {
          name: "App",
          components: VueShowdown,
          data: function() {
          return {
          fileContent: null,
          rawContent: null
          };
          },
          created: function() {
          // const fileToRender = `./assets/documentation/general/welcome.md`;
          //const rawContent = ""; // Read the file content using fileToRender
          // this.fileContent = "### marked(rawContent) should get executed";
          this.getContent();
          },
          methods: {
          getContent() {
          this.fileContent = MarkDownData;
          }
          }
          };
          </script>


          See: https://codesandbox.io/s/xpmy7pzyqz






          share|improve this answer


























          • I upvoted it because it works but I don't know if this is what I'm looking for. Your first sandbox: I want to read a local file without using a http request. Your second sandbox: Sorry, I need to read it on the fly. So the import would happen within the getContent() function multiple times...

            – Question3r
            Jan 2 at 20:22














          1












          1








          1







          Use VueResource to retrieve the content from your markdown file.
          Import the VueResource, and add it using Vue.use method (main.js):



          import Vue from "vue";
          import App from "./App";
          import VueResource from "vue-resource";

          Vue.config.productionTip = false;
          Vue.use(VueResource);

          new Vue({
          el: "#app",
          components: { App },
          template: "<App/>"
          });


          Then use this.$http.get() method it within your App.vue file to retrieve the markdown file conent.
          You can use markdown parsing library, like Showdown.js, wrapped within a vue.js method, directive or filter.



          See: https://github.com/showdownjs/showdown and http://showdownjs.com/



          There is also vuejs component wrapper for Showdown:
          See: https://github.com/meteorlxy/vue-showdown and https://vue-showdown.js.org/



          In your case that should look something like this ( using vue-showdown):



          <template>
          <div id="app"><VueShowdown :markdown="fileContent"></VueShowdown></div>
          </template>

          <script>
          import VueShowdown from "vue-showdown";

          export default {
          name: "App",
          components: VueShowdown,
          data: function() {
          return {
          fileContent: null,
          fileToRender:
          "https://gist.githubusercontent.com/rt2zz/e0a1d6ab2682d2c47746950b84c0b6ee/raw/83b8b4814c3417111b9b9bef86a552608506603e/markdown-sample.md",
          rawContent: null
          };
          },
          created: function() {
          // const fileToRender = `./assets/documentation/general/welcome.md`;
          //const rawContent = ""; // Read the file content using fileToRender
          // this.fileContent = "### marked(rawContent) should get executed";
          this.getContent();
          },
          methods: {
          getContent() {
          this.fileContent = "rendering ";
          // var self;
          this.$http.get(this.fileToRender).then(
          response => {
          // get body data

          this.fileContent = response.body;
          },
          response => {
          // error callback
          this.fileContent = "An error ocurred";
          }
          );
          }
          }
          };
          </script>


          Check in sandbox: https://codesandbox.io/s/poknq9z6q



          If your markdown file load is one time thing, then you could import it data, just like you import the components, js files and libraries:



          <template>
          <div id="app"><VueShowdown :markdown="fileContent"></VueShowdown></div>
          </template>

          <script>
          import VueShowdown from "vue-showdown";

          import MarkDownData from './assets/documentation/general/welcome.md';

          export default {
          name: "App",
          components: VueShowdown,
          data: function() {
          return {
          fileContent: null,
          rawContent: null
          };
          },
          created: function() {
          // const fileToRender = `./assets/documentation/general/welcome.md`;
          //const rawContent = ""; // Read the file content using fileToRender
          // this.fileContent = "### marked(rawContent) should get executed";
          this.getContent();
          },
          methods: {
          getContent() {
          this.fileContent = MarkDownData;
          }
          }
          };
          </script>


          See: https://codesandbox.io/s/xpmy7pzyqz






          share|improve this answer















          Use VueResource to retrieve the content from your markdown file.
          Import the VueResource, and add it using Vue.use method (main.js):



          import Vue from "vue";
          import App from "./App";
          import VueResource from "vue-resource";

          Vue.config.productionTip = false;
          Vue.use(VueResource);

          new Vue({
          el: "#app",
          components: { App },
          template: "<App/>"
          });


          Then use this.$http.get() method it within your App.vue file to retrieve the markdown file conent.
          You can use markdown parsing library, like Showdown.js, wrapped within a vue.js method, directive or filter.



          See: https://github.com/showdownjs/showdown and http://showdownjs.com/



          There is also vuejs component wrapper for Showdown:
          See: https://github.com/meteorlxy/vue-showdown and https://vue-showdown.js.org/



          In your case that should look something like this ( using vue-showdown):



          <template>
          <div id="app"><VueShowdown :markdown="fileContent"></VueShowdown></div>
          </template>

          <script>
          import VueShowdown from "vue-showdown";

          export default {
          name: "App",
          components: VueShowdown,
          data: function() {
          return {
          fileContent: null,
          fileToRender:
          "https://gist.githubusercontent.com/rt2zz/e0a1d6ab2682d2c47746950b84c0b6ee/raw/83b8b4814c3417111b9b9bef86a552608506603e/markdown-sample.md",
          rawContent: null
          };
          },
          created: function() {
          // const fileToRender = `./assets/documentation/general/welcome.md`;
          //const rawContent = ""; // Read the file content using fileToRender
          // this.fileContent = "### marked(rawContent) should get executed";
          this.getContent();
          },
          methods: {
          getContent() {
          this.fileContent = "rendering ";
          // var self;
          this.$http.get(this.fileToRender).then(
          response => {
          // get body data

          this.fileContent = response.body;
          },
          response => {
          // error callback
          this.fileContent = "An error ocurred";
          }
          );
          }
          }
          };
          </script>


          Check in sandbox: https://codesandbox.io/s/poknq9z6q



          If your markdown file load is one time thing, then you could import it data, just like you import the components, js files and libraries:



          <template>
          <div id="app"><VueShowdown :markdown="fileContent"></VueShowdown></div>
          </template>

          <script>
          import VueShowdown from "vue-showdown";

          import MarkDownData from './assets/documentation/general/welcome.md';

          export default {
          name: "App",
          components: VueShowdown,
          data: function() {
          return {
          fileContent: null,
          rawContent: null
          };
          },
          created: function() {
          // const fileToRender = `./assets/documentation/general/welcome.md`;
          //const rawContent = ""; // Read the file content using fileToRender
          // this.fileContent = "### marked(rawContent) should get executed";
          this.getContent();
          },
          methods: {
          getContent() {
          this.fileContent = MarkDownData;
          }
          }
          };
          </script>


          See: https://codesandbox.io/s/xpmy7pzyqz







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 17:51

























          answered Jan 2 at 16:37









          niklazniklaz

          1,99511217




          1,99511217













          • I upvoted it because it works but I don't know if this is what I'm looking for. Your first sandbox: I want to read a local file without using a http request. Your second sandbox: Sorry, I need to read it on the fly. So the import would happen within the getContent() function multiple times...

            – Question3r
            Jan 2 at 20:22



















          • I upvoted it because it works but I don't know if this is what I'm looking for. Your first sandbox: I want to read a local file without using a http request. Your second sandbox: Sorry, I need to read it on the fly. So the import would happen within the getContent() function multiple times...

            – Question3r
            Jan 2 at 20:22

















          I upvoted it because it works but I don't know if this is what I'm looking for. Your first sandbox: I want to read a local file without using a http request. Your second sandbox: Sorry, I need to read it on the fly. So the import would happen within the getContent() function multiple times...

          – Question3r
          Jan 2 at 20:22





          I upvoted it because it works but I don't know if this is what I'm looking for. Your first sandbox: I want to read a local file without using a http request. Your second sandbox: Sorry, I need to read it on the fly. So the import would happen within the getContent() function multiple times...

          – Question3r
          Jan 2 at 20:22




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009901%2fretrieve-content-from-markdown-file-and-convert-it-to-valid-html-code-in-vuejs%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          Npm cannot find a required file even through it is in the searched directory