Skip to content

Commit

Permalink
feat: added support for title & favicon
Browse files Browse the repository at this point in the history
  • Loading branch information
jd-solanki committed Jul 14, 2023
1 parent e874a1b commit a87a98d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@

Converts markdown files to HTML

Check action.yml for docs.
## Usage

Detailed readme coming soon.
```yml
- name: Convert index.md to index.html
uses: jd-solanki/gh-action-md-to-html@v1.3.0
with:
files: '[["index.md", "index.html"]]'
theme: 'dark' # Optional, default: auto
title: 'My Website' # Optional, If title isn't provided title tag won't get added
favicon: 'https://github.githubassets.com/favicons/favicon-dark.png' # Optional, If favicon isn't provided favicon's link tag won't get added
```

## ToDo

- [ ] Fetch title from repo name if isn't provided (PR welcome)

## Credits

Expand Down
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ inputs:
debug:
description: Boolean value which will tell action to log the action it performs. Useful for debugging.
default: false
title:
description: 'Title of the HTML page'
# TODO: Get it from repo name
required: false
favicon:
description: 'Favicon of the HTML page'
required: false
runs:
using: 'node16'
main: 'dist/index.js'
18 changes: 14 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12949,19 +12949,24 @@ const md = new MarkdownIt({
html: true,
})

async function mdToHtml(filePath, debug, theme) {
async function mdToHtml(filePath, debug, theme, options) {
// Read source file
const content = await fs.readFile(filePath, 'utf8')
if (debug) core.info(`content of ${filePath}: ${content}`)

// converted to HTML
const rendered = md.render(content)

const titleTag = options.title ? `<title>${options.title}</title>` : ''
const faviconTag = options.favicon ? `<link rel="icon" href="${options.favicon}" />` : ''

const html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Changelog</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
${faviconTag}
${titleTag}
<style>
${theme === 'light'
? themeLight
Expand Down Expand Up @@ -12997,10 +13002,14 @@ ${rendered}
// 👉 Get config
const debug = Boolean(JSON.parse(core.getInput('debug')))
const theme = core.getInput('theme')
const pageTitle = core.getInput('title')
const favicon = core.getInput('favicon')

if (debug) core.info(`debug: ${debug}`)

if (debug) core.info(`theme: ${theme}`)
if (debug) core.info(`pageTitle: ${pageTitle}`)
if (debug) core.info(`favicon: ${favicon}`)

const files = JSON.parse(core.getInput('files'))
if (debug) core.info(`files: ${files}`)
Expand All @@ -13022,8 +13031,9 @@ ${rendered}
if (debug) core.info(`Working on: ${JSON.stringify(files[index])}`)
if (debug) core.info(`writing ${files[index][1]}`)

if (debug) core.info(`HTML content to write: ${await mdToHtml(files[index][0], debug, theme)}`)
await fs.writeFile(files[index][1], await mdToHtml(files[index][0], debug, theme), err => { if (err) core.warning(err) })
const html = await mdToHtml(files[index][0], debug, theme, { title: pageTitle, favicon })
if (debug) core.info(`HTML content to write: ${html}`)
await fs.writeFile(files[index][1], html, err => { if (err) core.warning(err) })
if (debug) core.info('Written to HTML file')
}

Expand Down
18 changes: 14 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,24 @@ const md = new MarkdownIt({
html: true,
})

async function mdToHtml(filePath, debug, theme) {
async function mdToHtml(filePath, debug, theme, options) {
// Read source file
const content = await fs.readFile(filePath, 'utf8')
if (debug) core.info(`content of ${filePath}: ${content}`)

// converted to HTML
const rendered = md.render(content)

const titleTag = options.title ? `<title>${options.title}</title>` : ''
const faviconTag = options.favicon ? `<link rel="icon" href="${options.favicon}" />` : ''

const html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Changelog</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
${faviconTag}
${titleTag}
<style>
${theme === 'light'
? themeLight
Expand Down Expand Up @@ -59,10 +64,14 @@ ${rendered}
// 👉 Get config
const debug = Boolean(JSON.parse(core.getInput('debug')))
const theme = core.getInput('theme')
const pageTitle = core.getInput('title')
const favicon = core.getInput('favicon')

if (debug) core.info(`debug: ${debug}`)

if (debug) core.info(`theme: ${theme}`)
if (debug) core.info(`pageTitle: ${pageTitle}`)
if (debug) core.info(`favicon: ${favicon}`)

const files = JSON.parse(core.getInput('files'))
if (debug) core.info(`files: ${files}`)
Expand All @@ -84,8 +93,9 @@ ${rendered}
if (debug) core.info(`Working on: ${JSON.stringify(files[index])}`)
if (debug) core.info(`writing ${files[index][1]}`)

if (debug) core.info(`HTML content to write: ${await mdToHtml(files[index][0], debug, theme)}`)
await fs.writeFile(files[index][1], await mdToHtml(files[index][0], debug, theme), err => { if (err) core.warning(err) })
const html = await mdToHtml(files[index][0], debug, theme, { title: pageTitle, favicon })
if (debug) core.info(`HTML content to write: ${html}`)
await fs.writeFile(files[index][1], html, err => { if (err) core.warning(err) })
if (debug) core.info('Written to HTML file')
}

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a87a98d

Please sign in to comment.