Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable serving local files and fix browser history #614

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/out/404.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html><head><meta charSet="utf-8" class="next-head"/><link rel="preload" href="/_next/static/s7O4q0ISzv1r8jtwIkXLb/pages/404.js" as="script"/><link rel="preload" href="/_next/static/s7O4q0ISzv1r8jtwIkXLb/pages/_app.js" as="script"/><link rel="preload" href="/_next/static/s7O4q0ISzv1r8jtwIkXLb/pages/_error.js" as="script"/><link rel="preload" href="/_next/static/runtime/webpack-2ef50c24cc8d478adafc.js" as="script"/><link rel="preload" href="/_next/static/chunks/commons.eb02219a61ab5016dbbe.js" as="script"/><link rel="preload" href="/_next/static/runtime/main-3978781480e8c13aefab.js" as="script"/></head><body><div id="__next"><div data-reactroot="">404</div></div><script>__NEXT_DATA__ = {"props":{"pageProps":{}},"page":"/404","query":{},"buildId":"s7O4q0ISzv1r8jtwIkXLb","nextExport":true};__NEXT_LOADED_PAGES__=[];__NEXT_REGISTER_PAGE=function(r,f){__NEXT_LOADED_PAGES__.push([r, f])}</script><script async="" id="__NEXT_PAGE__/404" src="/_next/static/s7O4q0ISzv1r8jtwIkXLb/pages/404.js"></script><script async="" id="__NEXT_PAGE__/_app" src="/_next/static/s7O4q0ISzv1r8jtwIkXLb/pages/_app.js"></script><script async="" id="__NEXT_PAGE__/_error" src="/_next/static/s7O4q0ISzv1r8jtwIkXLb/pages/_error.js"></script><script src="/_next/static/runtime/webpack-2ef50c24cc8d478adafc.js" async=""></script><script src="/_next/static/chunks/commons.eb02219a61ab5016dbbe.js" async=""></script><script src="/_next/static/runtime/main-3978781480e8c13aefab.js" async=""></script></body></html>
<!DOCTYPE html><html><head><meta charSet="utf-8" class="next-head"/><link rel="preload" href="/_next/static/em0ZcIBUnglF46UE~66Iy/pages/404.js" as="script"/><link rel="preload" href="/_next/static/em0ZcIBUnglF46UE~66Iy/pages/_app.js" as="script"/><link rel="preload" href="/_next/static/em0ZcIBUnglF46UE~66Iy/pages/_error.js" as="script"/><link rel="preload" href="/_next/static/runtime/webpack-2ef50c24cc8d478adafc.js" as="script"/><link rel="preload" href="/_next/static/chunks/commons.eb02219a61ab5016dbbe.js" as="script"/><link rel="preload" href="/_next/static/runtime/main-3978781480e8c13aefab.js" as="script"/></head><body><div id="__next"><div data-reactroot="">404</div></div><script>__NEXT_DATA__ = {"props":{"pageProps":{}},"page":"/404","query":{},"buildId":"em0ZcIBUnglF46UE~66Iy","nextExport":true};__NEXT_LOADED_PAGES__=[];__NEXT_REGISTER_PAGE=function(r,f){__NEXT_LOADED_PAGES__.push([r, f])}</script><script async="" id="__NEXT_PAGE__/404" src="/_next/static/em0ZcIBUnglF46UE~66Iy/pages/404.js"></script><script async="" id="__NEXT_PAGE__/_app" src="/_next/static/em0ZcIBUnglF46UE~66Iy/pages/_app.js"></script><script async="" id="__NEXT_PAGE__/_error" src="/_next/static/em0ZcIBUnglF46UE~66Iy/pages/_error.js"></script><script src="/_next/static/runtime/webpack-2ef50c24cc8d478adafc.js" async=""></script><script src="/_next/static/chunks/commons.eb02219a61ab5016dbbe.js" async=""></script><script src="/_next/static/runtime/main-3978781480e8c13aefab.js" async=""></script></body></html>

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/out/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html><head><meta charSet="utf-8" class="next-head"/><title class="next-head"></title><link rel="shortcut icon" type="image/ico" href="/_static/favicon.ico" class="next-head"/><link rel="stylesheet" href="/_static/page.css" class="next-head"/><link rel="stylesheet" href="/_static/markdown.css" class="next-head"/><link rel="stylesheet" href="/_static/highlight.css" class="next-head"/><link rel="stylesheet" href="/_static/[email protected]" class="next-head"/><link rel="stylesheet" href="/_static/sequence-diagram-min.css" class="next-head"/><script type="text/javascript" src="/_static/underscore-min.js" class="next-head"></script><script type="text/javascript" src="/_static/webfont.js" class="next-head"></script><script type="text/javascript" src="/_static/snap.svg.min.js" class="next-head"></script><script type="text/javascript" src="/_static/tweenlite.min.js" class="next-head"></script><script type="text/javascript" src="/_static/mermaid.min.js" class="next-head"></script><script type="text/javascript" src="/_static/sequence-diagram-min.js" class="next-head"></script><script type="text/javascript" src="/_static/[email protected]" class="next-head"></script><script type="text/javascript" src="/_static/mhchem.min.js" class="next-head"></script><script type="text/javascript" src="/_static/[email protected]" class="next-head"></script><script type="text/javascript" src="/_static/[email protected]" class="next-head"></script><script type="text/javascript" src="/_static/viz.js" class="next-head"></script><script type="text/javascript" src="/_static/full.render.js" class="next-head"></script><link rel="preload" href="/_next/static/s7O4q0ISzv1r8jtwIkXLb/pages/index.js" as="script"/><link rel="preload" href="/_next/static/s7O4q0ISzv1r8jtwIkXLb/pages/_app.js" as="script"/><link rel="preload" href="/_next/static/s7O4q0ISzv1r8jtwIkXLb/pages/_error.js" as="script"/><link rel="preload" href="/_next/static/runtime/webpack-2ef50c24cc8d478adafc.js" as="script"/><link rel="preload" href="/_next/static/chunks/commons.eb02219a61ab5016dbbe.js" as="script"/><link rel="preload" href="/_next/static/runtime/main-3978781480e8c13aefab.js" as="script"/></head><body><div id="__next"><main data-theme=""><div id="page-ctn" contenteditable="false"><section class="markdown-body"></section></div></main></div><script>__NEXT_DATA__ = {"props":{"pageProps":{}},"page":"/","query":{},"buildId":"s7O4q0ISzv1r8jtwIkXLb","nextExport":true};__NEXT_LOADED_PAGES__=[];__NEXT_REGISTER_PAGE=function(r,f){__NEXT_LOADED_PAGES__.push([r, f])}</script><script async="" id="__NEXT_PAGE__/" src="/_next/static/s7O4q0ISzv1r8jtwIkXLb/pages/index.js"></script><script async="" id="__NEXT_PAGE__/_app" src="/_next/static/s7O4q0ISzv1r8jtwIkXLb/pages/_app.js"></script><script async="" id="__NEXT_PAGE__/_error" src="/_next/static/s7O4q0ISzv1r8jtwIkXLb/pages/_error.js"></script><script src="/_next/static/runtime/webpack-2ef50c24cc8d478adafc.js" async=""></script><script src="/_next/static/chunks/commons.eb02219a61ab5016dbbe.js" async=""></script><script src="/_next/static/runtime/main-3978781480e8c13aefab.js" async=""></script></body></html>
<!DOCTYPE html><html><head><meta charSet="utf-8" class="next-head"/><title class="next-head"></title><link rel="shortcut icon" type="image/ico" href="/_static/favicon.ico" class="next-head"/><link rel="stylesheet" href="/_static/page.css" class="next-head"/><link rel="stylesheet" href="/_static/markdown.css" class="next-head"/><link rel="stylesheet" href="/_static/highlight.css" class="next-head"/><link rel="stylesheet" href="/_static/[email protected]" class="next-head"/><link rel="stylesheet" href="/_static/sequence-diagram-min.css" class="next-head"/><script type="text/javascript" src="/_static/underscore-min.js" class="next-head"></script><script type="text/javascript" src="/_static/webfont.js" class="next-head"></script><script type="text/javascript" src="/_static/snap.svg.min.js" class="next-head"></script><script type="text/javascript" src="/_static/tweenlite.min.js" class="next-head"></script><script type="text/javascript" src="/_static/mermaid.min.js" class="next-head"></script><script type="text/javascript" src="/_static/sequence-diagram-min.js" class="next-head"></script><script type="text/javascript" src="/_static/[email protected]" class="next-head"></script><script type="text/javascript" src="/_static/mhchem.min.js" class="next-head"></script><script type="text/javascript" src="/_static/[email protected]" class="next-head"></script><script type="text/javascript" src="/_static/[email protected]" class="next-head"></script><script type="text/javascript" src="/_static/viz.js" class="next-head"></script><script type="text/javascript" src="/_static/full.render.js" class="next-head"></script><link rel="preload" href="/_next/static/em0ZcIBUnglF46UE~66Iy/pages/index.js" as="script"/><link rel="preload" href="/_next/static/em0ZcIBUnglF46UE~66Iy/pages/_app.js" as="script"/><link rel="preload" href="/_next/static/em0ZcIBUnglF46UE~66Iy/pages/_error.js" as="script"/><link rel="preload" href="/_next/static/runtime/webpack-2ef50c24cc8d478adafc.js" as="script"/><link rel="preload" href="/_next/static/chunks/commons.eb02219a61ab5016dbbe.js" as="script"/><link rel="preload" href="/_next/static/runtime/main-3978781480e8c13aefab.js" as="script"/></head><body><div id="__next"><main data-theme=""><div id="page-ctn" contenteditable="false"><section class="markdown-body"></section></div></main></div><script>__NEXT_DATA__ = {"props":{"pageProps":{}},"page":"/","query":{},"buildId":"em0ZcIBUnglF46UE~66Iy","nextExport":true};__NEXT_LOADED_PAGES__=[];__NEXT_REGISTER_PAGE=function(r,f){__NEXT_LOADED_PAGES__.push([r, f])}</script><script async="" id="__NEXT_PAGE__/" src="/_next/static/em0ZcIBUnglF46UE~66Iy/pages/index.js"></script><script async="" id="__NEXT_PAGE__/_app" src="/_next/static/em0ZcIBUnglF46UE~66Iy/pages/_app.js"></script><script async="" id="__NEXT_PAGE__/_error" src="/_next/static/em0ZcIBUnglF46UE~66Iy/pages/_error.js"></script><script src="/_next/static/runtime/webpack-2ef50c24cc8d478adafc.js" async=""></script><script src="/_next/static/chunks/commons.eb02219a61ab5016dbbe.js" async=""></script><script src="/_next/static/runtime/main-3978781480e8c13aefab.js" async=""></script></body></html>
2 changes: 1 addition & 1 deletion app/pages/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export default class PreviewPage extends React.Component {
}

componentDidMount() {
this.startSocket(parseFloat(window.location.pathname.split('/')[2]))
this.startSocket(parseFloat(window.location.pathname.split('/').pop()))
}

onConnect() {
Expand Down
28 changes: 27 additions & 1 deletion app/routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs')
const path = require('path')
const logger = require('./lib/util/logger')('app/routes')
const os = require('os')

const routes = []

Expand All @@ -10,7 +11,7 @@ const use = function (route) {

// /page/:number
use((req, res, next) => {
if (/\/page\/\d+/.test(req.asPath)) {
if (/\/page\/\d+/.test(req.asPath) || /^\/\d+$/.test(req.asPath)) {
return fs.createReadStream('./out/index.html').pipe(res)
}
next()
Expand Down Expand Up @@ -120,6 +121,31 @@ use(async (req, res, next) => {
next()
})

// catchall for local files
use(async (req, res, next) => {
logger.info('local file route: ', req.asPath)

let fileDir = await req.plugin.nvim.call('expand', `#${req.bufnr}:p:h`)
logger.info('fileDir', fileDir)

let filePath = decodeURIComponent(decodeURIComponent(req.asPath))
filePath = filePath.replace(/^\/~\//, os.homedir() + '/') // Expand ~/
filePath = filePath.replace(/\\ /g, ' ')

let filePathOptions = [
filePath, // absolute path
path.join(fileDir, filePath), // relative path
]
for (let i in filePathOptions) {
logger.info('filePath', filePathOptions[i])
if (fs.existsSync(filePathOptions[i]) && !fs.statSync(filePathOptions[i]).isDirectory()) {
return fs.createReadStream(filePathOptions[i]).pipe(res)
}
}

next()
})

// 404
use((req, res) => {
res.statusCode = 404
Expand Down