This repository has been archived by the owner on Sep 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Adds types - Converts to ESM - Uses [ejs](https://www.npmjs.com/package/ejs) for html templating
- Loading branch information
1 parent
b9a998a
commit 319e2b4
Showing
12 changed files
with
301 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,3 +62,4 @@ typings/ | |
# while testing npm5 | ||
package-lock.json | ||
yarn.lock | ||
types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,76 @@ | ||
'use strict' | ||
|
||
const filesize = require('filesize') | ||
|
||
const mainStyle = require('./style') | ||
const pathUtil = require('../utils/path') | ||
import filesize from 'filesize' | ||
import style from './style.js' | ||
import { cidArray } from '../utils/path.js' | ||
import ejs from 'ejs' | ||
|
||
/** | ||
* @param {string} path | ||
*/ | ||
function getParentHref (path) { | ||
const parts = pathUtil.cidArray(path).slice() | ||
const parts = cidArray(path).slice() | ||
if (parts.length > 1) { | ||
// drop the last segment in a safe way that works for both paths and urls | ||
return path.replace(`/${parts.pop()}`, '') | ||
} | ||
return path | ||
} | ||
|
||
function buildFilesList (path, links) { | ||
const rows = links.map((link) => { | ||
let row = [ | ||
'<div class="ipfs-icon ipfs-_blank"> </div>', | ||
`<a href="${path}${path.endsWith('/') ? '' : '/'}${link.Name}">${link.Name}</a>`, | ||
filesize(link.Tsize) | ||
] | ||
|
||
row = row.map((cell) => `<td>${cell}</td>`).join('') | ||
|
||
return `<tr>${row}</tr>` | ||
}) | ||
|
||
return rows.join('') | ||
} | ||
|
||
function buildTable (path, links) { | ||
return ` | ||
<table class="table table-striped"> | ||
<tbody> | ||
<tr> | ||
<td class="narrow"> | ||
<div class="ipfs-icon ipfs-_blank"> </div> | ||
</td> | ||
<td class="padding"> | ||
<a href="${getParentHref(path)}">..</a> | ||
</td> | ||
<td></td> | ||
</tr> | ||
${buildFilesList(path, links)} | ||
</tbody> | ||
</table> | ||
` | ||
} | ||
|
||
function render (path, links) { | ||
return ` | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>${path}</title> | ||
<style>${mainStyle}</style> | ||
</head> | ||
<body> | ||
<div id="header" class="row"> | ||
<div class="col-xs-2"> | ||
<div id="logo" class="ipfs-logo"></div> | ||
</div> | ||
/** | ||
* @param {string} path | ||
* @param {({ Name: string, Tsize: number })[]} links | ||
*/ | ||
export function render (path, links) { | ||
return ejs.render(`<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title><%= path %></title> | ||
<style>${style}</style> | ||
</head> | ||
<body> | ||
<div id="header" class="row"> | ||
<div class="col-xs-2"> | ||
<div id="logo" class="ipfs-logo"></div> | ||
</div> | ||
</div> | ||
<br> | ||
<div class="col-xs-12"> | ||
<div class="panel panel-default"> | ||
<div class="panel-heading"> | ||
<strong>Index of <%= path %></strong> | ||
</div> | ||
<br> | ||
<div class="col-xs-12"> | ||
<div class="panel panel-default"> | ||
<div class="panel-heading"> | ||
<strong>Index of ${path}</strong> | ||
</div> | ||
${buildTable(path, links)} | ||
</div> | ||
</div> | ||
</body> | ||
</html> | ||
` | ||
<table class="table table-striped"> | ||
<tbody> | ||
<tr> | ||
<td class="narrow"> | ||
<div class="ipfs-icon ipfs-_blank"> </div> | ||
</td> | ||
<td class="padding"> | ||
<a href="<%= parentHref %>">..</a> | ||
</td> | ||
<td></td> | ||
</tr> | ||
<% links.forEach(function (link) { %> | ||
<tr> | ||
<td><div class="ipfs-icon ipfs-_blank"> </div></td> | ||
<td><a href="<%= link.link %>"><%= link.name %></a></t> | ||
<td><%= link.size %></td> | ||
</td> | ||
</tr> | ||
<% }) %> | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
</body> | ||
</html> | ||
`, { | ||
path, | ||
links: links.map((link) => ({ | ||
name: link.Name, | ||
size: filesize(link.Tsize), | ||
link: `${path}${path.endsWith('/') ? '' : '/'}${link.Name}` | ||
})), | ||
parentHref: getParentHref(path) | ||
}) | ||
} | ||
|
||
exports.render = render |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.