How to extract only text from Markdown files in Javascript

Brett Fisher

I'm creating a simple blogging platform using Vue that serves up Markdown (*.md) files for the posts. On the main page, I want to show a list of the published posts as well as a preview of the first 30 words in each post. This is the function I have so far to show the preview (front-matter just parses out some meta data I have at the top of of the file, and postData just contains the text from the Markdown file):

import fm from "front-matter";

function postPreview() {
  var fmData = fm(postData).body;
  var words = fmData.split(" ");
  return words.slice(0, 30).join(" ");
}

The problem is, if the Markdown has image tags or link tags, then it displays those when I just want to display the raw text. For example, if this is my Markdown file:

![alt-text...](link-to-some-picture) Here is a [link](link-to-some-website) in my file.

Then the preview should just look like this:

Here is a link in my file.

Is there some library that will let me do this?

jeffld

There is probably a more direct way, but here is something that seems to work.

https://github.com/showdownjs/showdown

Here is a markdown to HTML script that works, then use jQuery to extract text() from the hidden element.

function Markdown2HTML(sInput){
   var  converter = new showdown.Converter();
   var  html = converter.makeHtml(sInput);
  return html ;
}

function fnProcess() {
  
  var sMarkDown = $("#myInput").val();
  
  var sHTML = Markdown2HTML(sMarkDown);
  
  $("#resultTemp").html(sHTML);

  $("#resultArea").html($("#resultTemp").text());

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/1.9.0/showdown.min.js"></script>

<button onclick="fnProcess()">process</button>

<input id="myInput" style="width:500px;" type="text" value="![alt-text...](link-to-some-picture) Here is a [link](link-to-some-website) in my file." />
<div id="resultArea" style="padding:10px;"></div>
<div id="resultTemp" style="display:none"></div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Extract only text files from a zip file

How to extract only text from HTML in Golang?

How to extract links from markdown

How to only extract only organization names from text using spacy

extract text from pdf files

How to extract only specific part from xml files and merge them?

How to extract only mp3 files from a ZIP archive

Promises/Fetch in JavaScript: how to extract text from text file

How to extract only the last two lines of text from a string?

Scrapy / XPATH : how to extract ONLY text from descendants and self

How to extract only text from HTML string with PHP?

How do I extract only number from dynamic headline/text?

How to extract text from a directory of PDF files efficiently with OCR?

How to extract uncommon case insensitive text from two files?

How to extract text from PDF files and save as CSV using python

how to extract text from docx files contaning in different folders

How to extract only minus and plus operation symbols from a string in JavaScript

extract text from pdf in Javascript

How to extract more than one text from the string with javascript?

How can I extract text from the middle of a string with Javascript?

How to keep only wanted text from XML files using vba

how to browse only text files from my application

Extract only text from Rmd documents

Extract only bold text from PDF documents

Extract text only from the first square brackets

Extract only number from text with Regex and Notepad ++

Matlab script to extract data from text files

Extract data from text files using for loop

Extract sentence from text files in Python

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive