Skip to content

Commit

Permalink
2.0.3 new logger, robots.txt, experimental html-export
Browse files Browse the repository at this point in the history
  • Loading branch information
wendevlin committed Jun 3, 2024
1 parent 5e3135f commit feebbd8
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 14 deletions.
6 changes: 6 additions & 0 deletions homedocs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<!-- https://developers.home-assistant.io/docs/add-ons/presentation#keeping-a-changelog -->

## 2.0.3

- Add robots.txt disallowing all bots
- improve logs with date and time
- Experimental: Add html export all pages at once to print it to PDF

## 2.0.2

- Fixed serving of assets
Expand Down
2 changes: 1 addition & 1 deletion homedocs/config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Homedocs

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config

Check warning on line 1 in homedocs/config.yaml

View workflow job for this annotation

GitHub Actions / Lint add-on homedocs

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config
description: Document stuff with Markdown
version: "2.0.2"
version: "2.0.3"
slug: homedocs
init: false
url: https://github.com/masterwendu/homeassistant-addons/tree/main/homedocs
Expand Down
Binary file modified homedocs/server/bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions homedocs/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"glob": "10.4.1",
"markdown-it": "14.1.0",
"markdown-it-emoji": "3.0.0",
"pino": "9.1.0",
"pino-pretty": "11.1.0",
"zod": "3.23.8"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions homedocs/server/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *
Disallow: /
5 changes: 3 additions & 2 deletions homedocs/server/scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// import { build } from "bun"
import logger from "../src/utils/logger"
import { buildCss } from "./tailwind"

// build css with tailwind and daisyui
await buildCss()

console.log('CSS build finished ✓')
logger.info('CSS build finished ✓')

// build the server
// TODO use optimized bun build
Expand All @@ -14,4 +15,4 @@ console.log('CSS build finished ✓')
// target: 'bun',
// })

console.log('Server build finished ✓')
logger.info('Server build finished ✓')
7 changes: 4 additions & 3 deletions homedocs/server/src/fileWatcher.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chokidar from 'chokidar'
import { buildDocs } from './htmlBuilder'
import environmentVariables from './utils/environmentVariables'
import logger from './utils/logger'

let timeout: Timer | null = null

Expand All @@ -10,9 +11,9 @@ const triggerNewBuild = async () => {
}

timeout = setTimeout(async () => {
console.log('Triggering new build')
logger.info('Change detected')
await buildDocs()
console.log('Docs rebuilt')
logger.info('Docs rebuilt')
}, 1000)
}

Expand All @@ -21,7 +22,7 @@ export const startWatcher = () => {
// TODO do not rebuild everything, only if needed
triggerNewBuild()
})
console.log(
logger.info(
`Watcher started, watching for changes in "${environmentVariables.docsBasePath}" folder`,
)
}
71 changes: 68 additions & 3 deletions homedocs/server/src/htmlBuilder/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { $ } from 'bun'
import { $, FileSink } from 'bun'
import layout from '../templates/layout'
import { generateNavigation } from '../templates/navigation'
import environmentVariables from '../utils/environmentVariables'
import { generateNavForFolder } from './navigation'
import type { NavigationEntry } from './types'
import type { Breadcrumb, NavigationEntry } from './types'
import layoutTop from '../templates/merged/layoutTop'
import layoutBottom from '../templates/merged/layoutBottom'
import page from '../templates/merged/page'
import logger from '../utils/logger'

const buildHtml = async (
navigationEntry: NavigationEntry,
Expand All @@ -21,7 +25,7 @@ const buildHtml = async (
).toString()
const fileName = `./dist/docs/${navigationEntry.path || 'index'}.html`
await Bun.write(fileName, `<!doctype html>${html}`)
console.log(`Built ${fileName}`)
logger.info(`Built ${fileName}`)
}

const buildLevel = async (
Expand Down Expand Up @@ -49,3 +53,64 @@ export const buildDocs = async () => {

await buildLevel(navigationStructure, navigationStructure)
}

const buildLevelInOnePage = (
writer: FileSink,
navigationStructure: NavigationEntry[],
breadcrumbs: Breadcrumb[] = [],
) => {
for (const navigationEntry of navigationStructure) {
if (typeof navigationEntry.htmlContent === 'string') {
writer.write(page(
navigationEntry.title,
navigationEntry.htmlContent,
[
...breadcrumbs,
{
name: navigationEntry.title,
isFolder: false,
},
],
).toString())
writer.flush()
}

if (navigationEntry.children) {
buildLevelInOnePage(
writer,
navigationEntry.children,
[
...breadcrumbs,
{
name: navigationEntry.title,
isFolder: navigationEntry.htmlContent === undefined,
},
],
)
}
}
}

export const buildDocsInOnePage = async () => {
const navigationStructure = await generateNavForFolder(
environmentVariables.docsBasePath,
)

await Bun.$`rm ./dist/mergedDocs.html`.quiet()

const file = Bun.file('./dist/mergedDocs.html')
const writer = file.writer();

writer.write(layoutTop)
writer.flush()

buildLevelInOnePage(writer, navigationStructure)

writer.write(layoutBottom)
writer.flush()
writer.end()

return file
}

await buildDocsInOnePage()
5 changes: 5 additions & 0 deletions homedocs/server/src/htmlBuilder/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ export type NavigationEntry = {
children?: NavigationEntry[]
pathPrefix?: string
}

export type Breadcrumb = {
name: string
isFolder: boolean
}
9 changes: 5 additions & 4 deletions homedocs/server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { buildCss } from '../scripts/tailwind'
import { startWatcher } from './fileWatcher'
import logger from './utils/logger'
import { webserver } from './webserver'

// 2 parts
Expand All @@ -13,15 +14,15 @@ import { webserver } from './webserver'

// if dev mode only rebuild css
if (process.env.NODE_ENV !== 'production') {
console.log('Homedocs is running in development mode')
logger.info('Homedocs is running in development mode')
await buildCss()
console.log('Css built')
logger.info('Css built')
}

startWatcher()
// await buildDocs()
// console.log('Initial docs built')
// logger.info('Initial docs built')

webserver.listen(3000)

console.log(`🦊 Elysia is running at ${webserver.server?.url}`)
logger.info(`🦊 Elysia is running at ${webserver.server?.url}`)
5 changes: 5 additions & 0 deletions homedocs/server/src/templates/merged/layoutBottom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default `
</div>
</body>
</html>
`
18 changes: 18 additions & 0 deletions homedocs/server/src/templates/merged/layoutTop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default `
<html lang="en">
<head>
<title>Homedocs</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="./main.css" />
<link rel="icon" href="./favicon.svg" />
</head>
<body>
<div class="navbar bg-nav dark:bg-nav-dark dark:text-white justify-between">
<span class="btn btn-ghost text-xl">
<img class="h-8 mr-5" src="./favicon.svg" alt="Homedocs Logo" />
Homedocs
</span>
</div>
<div class="flex flex-col items-center justify-start">
`
28 changes: 28 additions & 0 deletions homedocs/server/src/templates/merged/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Breadcrumb } from "../../htmlBuilder/types";

export default (
title: string,
content: string,
breadcrumbs: Breadcrumb[],
) => (
<div class="py-10 px-3 max-w-screen-xl w-full">
<div class="text-sm breadcrumbs">
<ul>
{breadcrumbs.map((breadcrumb) => (
<li>
<div class="badge badge-outline">
{breadcrumb.isFolder ? (
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="w-4 h-4 stroke-current"><title>Folder icon</title><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"></path></svg>
) : ''}
{breadcrumb.name}
</div>
</li>
))}
</ul>
</div>
<div class="prose">
{content}
</div>
<div class="divider"></div>
</div>
)
2 changes: 1 addition & 1 deletion homedocs/server/src/templates/navigation.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NavigationEntry } from '../utils/types'
import type { NavigationEntry } from '../htmlBuilder/types'

const subNavigation = (
navigation: NavigationEntry[],
Expand Down
9 changes: 9 additions & 0 deletions homedocs/server/src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pino from 'pino'
import pinoPretty from 'pino-pretty'

const logger = pino(pinoPretty({
translateTime: 'yyyy-mm-dd HH:MM:ss.l',
ignore: 'pid,level,hostname'
}))

export default logger
5 changes: 5 additions & 0 deletions homedocs/server/src/webserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { BunFile } from 'bun'
import { Elysia } from 'elysia'
import notFoundPage from './templates/404'
import environmentVariables from './utils/environmentVariables'
import { buildDocsInOnePage } from './htmlBuilder'

export const webserver = new Elysia({
serve: {
Expand All @@ -14,8 +15,12 @@ export const webserver = new Elysia({
const fileName = path.split('/').pop() ?? ''
if (fileName === 'main.css') {
return Bun.file('./dist/main.css')
} else if (fileName === 'robots.txt') {
return Bun.file('./public/robots.txt')
} else if (fileName.startsWith('favicon')) {
return Bun.file('./public/favicon.svg')
} else if (path === '/html-export') {
return buildDocsInOnePage()
} else if (
fileName.includes('.') &&
(fileName.split('.').pop()?.length ?? 0) > 1
Expand Down

0 comments on commit feebbd8

Please sign in to comment.