Skip to content

Commit

Permalink
Bug fixes: import this package in Typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
ksw2000 committed May 9, 2023
1 parent d8734f7 commit c7cb958
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gitpage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
run: |
npm ci
tsc
node dist/index.js -s ./example
node dist/cli.js -s ./example
- name: commit and push
run: |
cd output
Expand Down
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@
+ Support render CSV as table
+ Support PlantUML
+ `v0.0.11`
+ Support fretboard
+ Support fretboard
+ `v0.0.12`
+ Change the exported class `Covert` to `Converter`
+ Now we can import this package by `import { Converter } from 'hackmd-to-html-cli'`
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

Not only is this a CLI tool, but it is also an importable package for converting standard Markdown and even [HackMD](https://hackmd.io/)-supported Markdown into HTML.

+ See the example of input markdown: [./example/index.md](https://raw.githubusercontent.com/ksw2000/hackmd-to-html-cli/main/example/index.md)
+ Example of input markdown: [./example/index.md](https://raw.githubusercontent.com/ksw2000/hackmd-to-html-cli/main/example/index.md)

+ See the example of output html: [https://ksw2000.github.io/hackmd-to-html-cli/](https://ksw2000.github.io/hackmd-to-html-cli/)
+ Example of output html: [https://ksw2000.github.io/hackmd-to-html-cli/](https://ksw2000.github.io/hackmd-to-html-cli/)

## Install

Expand Down Expand Up @@ -56,10 +56,12 @@ $ hmd2html -s hello.md -l ./myLayout.html
## Package (beta)

```js
const {Convert} = require('hackmd-to-html-cli')
// for TypeScript
// import { Converter } from 'hackmd-to-html-cli'
const { Converter } = require('hackmd-to-html-cli')
const template = `{{main}}`
const hardBreak = true
const converter = new Convert(template, hardBreak)
const converter = new Converter(template, hardBreak)
const md = `
# title
hello world
Expand All @@ -69,23 +71,22 @@ console.log(converter.convert(md))

**output**

```
```html
<h1 id="title" tabindex="-1">title</h1>
<p>hello world</p>
```

If you want to get default layout
Some features

```js
convert.defaultLayout()
```

If you want to get metadata after converting
// get default layout
converter.defaultLayout()

```js
// get metadata after converting
converter.getMetadata()
```


## Layout

See default layout here: https://github.com/ksw2000/hackmd-to-html-cli/blob/main/layout.html
Expand Down
22 changes: 22 additions & 0 deletions lib/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env node
import commander from 'commander'
import fs from 'fs'
import { Converter } from './converter'

commander.program.version('0.0.12', '-v, --version', 'output the current version')
commander.program
.requiredOption('-s, --source <files_or_dirs...>', 'specify the input markdown files or directories')
.addOption(new commander.Option('-d, --destination <path>', 'specify the output directory').default('./output', './output'))
.addOption(new commander.Option('-l, --layout <html_file>', 'specify the layout file').default('', '""'))
.addOption(new commander.Option('-b, --hardBreak', 'use hard break instead of soft break'))
.parse(process.argv)

const options = commander.program.opts()

const dest: string = options.destination === '' ? './output' : options.destination
const layout: string | null = options.layout !== '' ? fs.readFileSync(options.layout, { encoding: 'utf-8' }) : null
const hardBreak: boolean = options.hardBreak

const converter = new Converter(layout, hardBreak)

converter.convertFiles(options.source, dest)
16 changes: 8 additions & 8 deletions lib/converter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fs from 'fs'
import path from 'path'
import { MarkdownItYAMLMetadata, Metadata } from './yamlMetadata'
import { MarkdownItContainer } from './container'
import { MarkdownItCheckbox } from './checkbox'
import { MarkdownItExternal } from './external'
import { MarkdownItBlockquoteX } from './blockquotex'
import { MarkdownItFenceX } from './fenceX'
import { MarkdownItYAMLMetadata, Metadata } from './markdown/yamlMetadata'
import { MarkdownItContainer } from './markdown/container'
import { MarkdownItCheckbox } from './markdown/checkbox'
import { MarkdownItExternal } from './markdown/external'
import { MarkdownItBlockquoteX } from './markdown/blockquotex'
import { MarkdownItFenceX } from './markdown/fencex'
import MarkdownIt from 'markdown-it/lib'

const MarkdownItSub = require('markdown-it-sub')
Expand All @@ -23,7 +23,7 @@ const MarkdownItAnchor = require('markdown-it-anchor')
const MarkdownItRuby = require('markdown-it-ruby')
const htmlEncode = require('htmlencode').htmlEncode;

export class Convert {
export class Converter {
private md: MarkdownIt
private metadata: Metadata
private layout: string
Expand All @@ -32,7 +32,7 @@ export class Convert {
* @param layout set null if you want to use default layout,
* @param hardBreak set true if want to use hardBread
*/
constructor(layout: string | null, hardBreak: boolean = false) {
constructor(layout: string | null, hardBreak = false) {
this.metadata = new Metadata()
if (layout === null) {
layout = this.defaultLayout()
Expand Down
23 changes: 1 addition & 22 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1 @@
#!/usr/bin/env node
import commander from 'commander'
import fs from 'fs'
import { Convert } from './converter'

commander.program.version('0.0.11', '-v, --version', 'output the current version')
commander.program
.requiredOption('-s, --source <files_or_dirs...>', 'specify the input markdown files or directories')
.addOption(new commander.Option('-d, --destination <path>', 'specify the output directory').default('./output', './output'))
.addOption(new commander.Option('-l, --layout <html_file>', 'specify the layout file').default('', '""'))
.addOption(new commander.Option('-b, --hardBreak', 'use hard break instead of soft break'))
.parse(process.argv)

const options = commander.program.opts()

const dest: string = options.destination === '' ? './output' : options.destination
const layout: string | null = options.layout !== '' ? fs.readFileSync(options.layout, { encoding: 'utf-8' }) : null
const hardBreak: boolean = options.hardBreak

const converter = new Convert(layout, hardBreak)

converter.convertFiles(options.source, dest)
export {Converter} from './converter'
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions lib/fenceX.ts → lib/markdown/fencex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ export function MarkdownItFenceX(md: MarkdownIt, _options: MarkdownIt.Options) {
state.tokens = md.utils.arrayReplaceAt(state.tokens, i, newTokens)
} else if (params[0] === 'fretboard') {
const config = parseUserDefinedConfig(params.slice(1).join(' '))
let title = config.get('title') as string
let type = config.get('type') as string
let rendered = renderFretBoard(token.content, { title: title, type: type })
const title = config.get('title') as string
const type = config.get('type') as string
const rendered = renderFretBoard(token.content, { title: title, type: type })
const fret = new Token('html_block', 'div', 0)
fret.content = rendered

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hackmd-to-html-cli",
"version": "0.0.11",
"version": "0.0.12",
"description": "A node.js CLI tool for converting HackMD markdown to HTML.",
"keywords": [
"hackmd",
Expand All @@ -15,10 +15,10 @@
"url": "https://github.com/ksw2000/hackmd-to-html-cli.git"
},
"bin": {
"hmd2html": "dist/index.js"
"hmd2html": "dist/cli.js"
},
"main": "dist/converter.js",
"types": "types/converter.d.ts",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"test": "tsc & npx hmd2html -s ./example",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
"sourceRoot": "./lib", /* Specify the root path for debuggers to find the reference source code. */
"sourceRoot": "./lib/**/*", /* Specify the root path for debuggers to find the reference source code. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
"inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
Expand Down

0 comments on commit c7cb958

Please sign in to comment.