0.4.63 • Published 2 years ago

md-convert v0.4.63

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
2 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

2 years ago

0.4.43

2 years ago

0.4.40

2 years ago

0.4.41

2 years ago

0.4.48

2 years ago

0.4.49

2 years ago

0.4.47

2 years ago

0.4.44

2 years ago

0.4.45

2 years ago

0.4.31

2 years ago

0.4.32

2 years ago

0.4.30

2 years ago

0.4.39

2 years ago

0.4.37

2 years ago

0.4.38

2 years ago

0.4.35

2 years ago

0.4.36

2 years ago

0.4.33

2 years ago

0.4.34

2 years ago

0.4.20

2 years ago

0.4.21

2 years ago

0.4.62

2 years ago

0.4.63

2 years ago

0.4.60

2 years ago

0.4.61

2 years ago

0.4.28

2 years ago

0.4.29

2 years ago

0.4.26

2 years ago

0.4.27

2 years ago

0.4.24

2 years ago

0.4.25

2 years ago

0.4.22

2 years ago

0.4.23

2 years ago

0.4.53

2 years ago

0.4.54

2 years ago

0.4.51

2 years ago

0.4.52

2 years ago

0.4.50

2 years ago

0.4.17

2 years ago

0.4.18

2 years ago

0.4.59

2 years ago

0.4.16

2 years ago

0.4.57

2 years ago

0.4.58

2 years ago

0.4.55

2 years ago

0.4.56

2 years ago

0.4.9

2 years ago

0.4.8

2 years ago

0.4.10

2 years ago

0.4.15

2 years ago

0.4.13

2 years ago

0.4.14

2 years ago

0.4.11

2 years ago

0.4.12

2 years ago

0.4.5

2 years ago

0.4.7

2 years ago

0.4.6

2 years ago

0.4.4

2 years ago

0.4.3

2 years ago

0.4.2

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago