#How Can I Use Markdown With Javascript?

1 messages · Page 1 of 1 (latest)

slim dagger
#

Context: My site has several pages of mostly text. I want a faster and more maintainable way to write each page (while still having links, italics, etc.). I think a markdown system might be best for this. My plan is to have each page have a corresponding text file full of markdown saved on the server which gets loaded in when the user clicks on the respective links. I want to do this by loading in data into the current page instead of linking to a different page so that the sidebar, scroll positions, and other things aren't reset.

Question: What is the best way (secure, works on every browser, is performant) to open that text data, parse it into some kind of HTML, and replace the body element with that?

Attempts: I've tried looking up working with local files on Stack Overflow and stuff but most of it seems to be getting files from the USER'S device instead of the server. And a lot of it is deprecated or uses dependencies that I don't understand. I dunno. I have been able to replace the body element's HTML with HTML inside of a string in JavaScript, so if I can just figure out how to load and parse markdown text files then my problem will be solved. I'm asking ChatGPT about this right now and it's pointing me in the direction of Node.js and other backend stuff. I have never worked with Node.js but it looks like the right place to start.

Experience: I am brand new to JavaScript and have little experience with web development, but I have done programming with other languages for many years.

I appreciate any help.

thorn carbon
#

You have two options

  1. make each markdown file available as a page on your website. The JavaScript then fetches each markdown file as it needs them.
  2. embed the markdown in your JavaScript/HTML as strings/HTML elements.
#

Accessing a file on the server from a browser is what fetch is doing btw. Any time you make a request to a web server your essentially just asking for the contents of a file. Web servers have evolved to the point where we think of it more as sending content but at its core is just sending dynamically generated files.

slim dagger
#

Yes, I have been trying to do #1 @thorn carbon

#

I previously had the HTML (parsed from my own proprietary markdown with a C++ program I wrote) embedded as strings, but it's awkward to maintain that.