diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..1df25f7 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,30 @@ +name: CI + +on: + push: + branches: [ main, master ] + pull_request: + branches: [ main, master ] + +jobs: + test: + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + node-version: [16.x, 18.x, 20.x, 22.x, 24.x] + + steps: + - uses: actions/checkout@v4 + + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v6 + with: + node-version: ${{ matrix.node-version }} + + - name: Install dependencies + run: npm install + + - name: Run tests + run: npm test diff --git a/bin/darko-serve b/bin/darko-serve index e6d930c..0ddcc48 100755 --- a/bin/darko-serve +++ b/bin/darko-serve @@ -3,6 +3,7 @@ 'use strict' const program = require('commander') +const mime = require('mime-types') program .option('-s --source [source]', 'Source directory (default to ./)', './') @@ -77,12 +78,7 @@ function handle(req, res) { function sendFile(fpath) { debug('Sending ' + fpath) - const mime = { - '.css': 'text/css', - '.html': 'text/html', - '.js': 'application/javascript' - } - const contentType = mime[path.extname(fpath).toLowerCase()] + const contentType = mime.contentType(path.extname(fpath).toLowerCase()) if (contentType) { res.setHeader('Content-Type', contentType) @@ -115,6 +111,11 @@ function handle(req, res) { return } + if (!(fpath.startsWith(site.dest) || fpath.startsWith(droot))) { + send404() + return + } + const stats = fs.statSync(fpath) if (stats.isFile()) { diff --git a/lib/parsers/page.js b/lib/parsers/page.js index 4fedda6..e8b66a0 100644 --- a/lib/parsers/page.js +++ b/lib/parsers/page.js @@ -16,7 +16,7 @@ function Page(attrs) { this.ext = path.extname(fpath) this.slug = path.basename(fpath, this.ext) - this.path = path.relative(this.site.cwd, fpath) + this.path = path.relative(this.site.cwd, fpath).replaceAll(path.sep, '/') this.title = util.capitalize(this.slug) if (this.validFormat && fs.existsSync(fpath)) { @@ -33,7 +33,7 @@ function Page(attrs) { } } - this.url = path.resolve('/', this.path).replace(/\/index\.(?:md|html)$/, '') + this.url = `/${this.path}`.replace(/\/index\.(?:md|html)$/, '') this.dest = path.join(this.site.dest, this.site.baseurl.slice(1), this.path.replace(/\.\w+$/, this.ext == '.md' ? '.html' : this.ext)) diff --git a/lib/writers/templated.js b/lib/writers/templated.js index b03d3e3..82ebc72 100644 --- a/lib/writers/templated.js +++ b/lib/writers/templated.js @@ -4,7 +4,6 @@ const path = require('path') const yaml = require('yaml-js') const mkdirp = require('mkdirp') const debug = require('debug')('darko') -const util = require('util') const fs = require('fs') const md = require('../markdown') diff --git a/package.json b/package.json index 4c335cd..92d0de1 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "highlight.js": "^11.11.1", "liquid-node": "^3.0.0", "markit": "~0.1.0", + "mime-types": "^3.0.1", "mkdirp": "~0.3.5", "rimraf": "^2.6.2", "strftime": "~0.9.0", diff --git a/test/fixture/assets/darko.svg b/test/fixture/assets/darko.svg new file mode 100644 index 0000000..0f010a6 --- /dev/null +++ b/test/fixture/assets/darko.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/fixture/index.md b/test/fixture/index.md index 803965c..fe7db78 100644 --- a/test/fixture/index.md +++ b/test/fixture/index.md @@ -3,6 +3,8 @@ layout: post title: Welcome! --- + + Good luck, have fun!