0.4.63 • Published 3 years ago

md-convert v0.4.63

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
3 years ago

md-convert

Convert an HTML file to Markdown

Install

npm install [-g] md-convert

Usage

htmlmd --src <html-file> [--config <config-file>]

Where:

<html-file> - any valid HTML file
<config-file> - a JSON file to configure the conversion process (optional)

JavaScript:

import { htmlFileToMarkdownFile } from 'md-convert';
const htmlPath = 'index.html';
const configPath = 'my-mdconfig.json';
const turndownOptions = {
  headingStyle: 'atx',
  hr: '---',
  bulletListMarker: '-',
  codeBlockStyle: 'fenced',
  emDelimiter: '*'
};

const mdPath = htmlFileToMarkdownFile(htmlPath, configPath, turndownOptions);

API

readHtmlFile

/**
 * read HTML file and return HTMLDocument
 * @param {String} path
 * @return {HTMLDocument}
 */

const document = readHtmlFile(htmlPath);

readConfigFile

/**
 * read JSON config file and return MdcConfig object
 * @param {String} path
 * @return {MdcConfig}
 */

const config = readConfigFile(configPath);

createFrontMatter

/**
 * extract front matter from document
 * @param {HTMLDocument} document
 * @param {FrontMatterConfig} fmConfig
 * @return {FrontMatterString}
 */

const frontMatterStr = createFrontMatter(document, frontMatterConfig);

removeElements

/**
 * remove matching elements from document
 * @param {HTMLDocument} document
 * @param {CSSSelector} selector
 */

removeElements(document, omitConfig);

transformElements

/**
 * transform matching elements in document
 * @param {HTMLDocument} document
 * @param {Transformers} transformers
 */

transformElements(document, transformConfig);

elementsToMarkdown

/**
 * convert matching elements to markdown
 * @param {HTMLDocument} document
 * @param {CSSSelector} selector
 * @param {TurndownOptions} tdOptions
 * @return {MarkdownStr}
 */

const markdownText = elementsToMarkdown(
    document,
    cssSelector,
    turndownOptions
  );

htmlFileToMarkdownFile

/**
 * convert html file to markdown file
 * @param {String} htmlPath
 * @param {String} configPath
 * @param {TurndownOptions} tdOptions
 * @return {string} markdown file path
 */

const mdPath = htmlFileToMarkdownFile(htmlPath, configPath, turndownOptions);

JSON Config File Properties

{
  "description": "",
  "frontMatter": {},
  "omit": [],
  "transform": [],
  "select": []
}

Config Properties

  • description - describe what this configuration is for
  • frontMatter - how to generate "front matter" for markdown file
  • omit - CSS selector - HTML elements to remove from document
  • transform - rules to transform elements
  • select - CSS selector - HTML elements to include in conversion

Order of actions are:

  1. omit - remove elements from documemt
  2. transform - modify certain elements
  3. select - select elements for conversion

frontMatter Properties

{
  "<item_name>": {
  "selector": "<any_css_selector>",
  "attrName": "<element_attribute_name_if_any>"
}

If "attrName" is omitted, front matter content is obtained from element's textContent.

Example

HTML:

<title>Hello World Title</title>
<meta property="og:url" content="https://helloworld.com/helloworld.html">

Config:

{
  "frontMatter": {
    "title": {
      "selector": "title"
    },
    "url": {
      "selector": "meta[property='og:url']",
      "attrName": "content"
    }
  }
}

Resulting Front Matter:

---
title: Hello World Title  
url: https://helloworld.com/helloworld.html  
---

(more details to follow, until then, see config directory for examples)

0.4.42

3 years ago

0.4.43

3 years ago

0.4.40

3 years ago

0.4.41

3 years ago

0.4.48

3 years ago

0.4.49

3 years ago

0.4.47

3 years ago

0.4.44

3 years ago

0.4.45

3 years ago

0.4.31

3 years ago

0.4.32

3 years ago

0.4.30

3 years ago

0.4.39

3 years ago

0.4.37

3 years ago

0.4.38

3 years ago

0.4.35

3 years ago

0.4.36

3 years ago

0.4.33

3 years ago

0.4.34

3 years ago

0.4.20

3 years ago

0.4.21

3 years ago

0.4.62

3 years ago

0.4.63

3 years ago

0.4.60

3 years ago

0.4.61

3 years ago

0.4.28

3 years ago

0.4.29

3 years ago

0.4.26

3 years ago

0.4.27

3 years ago

0.4.24

3 years ago

0.4.25

3 years ago

0.4.22

3 years ago

0.4.23

3 years ago

0.4.53

3 years ago

0.4.54

3 years ago

0.4.51

3 years ago

0.4.52

3 years ago

0.4.50

3 years ago

0.4.17

3 years ago

0.4.18

3 years ago

0.4.59

3 years ago

0.4.16

3 years ago

0.4.57

3 years ago

0.4.58

3 years ago

0.4.55

3 years ago

0.4.56

3 years ago

0.4.9

3 years ago

0.4.8

3 years ago

0.4.10

3 years ago

0.4.15

3 years ago

0.4.13

3 years ago

0.4.14

3 years ago

0.4.11

3 years ago

0.4.12

3 years ago

0.4.5

3 years ago

0.4.7

3 years ago

0.4.6

3 years ago

0.4.4

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago