-
Notifications
You must be signed in to change notification settings - Fork 1
WIP, updating server.js to use JSDOM to fix isExternal, but also some… #3
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
Changes from all commits
0a8deed
ed08428
bbeaafd
8dd273b
2bc8253
22f1b2b
f18b994
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,36 @@ | ||
var rollup = require('rollup') | ||
var buble = require('rollup-plugin-buble') | ||
var async = require('rollup-plugin-async') | ||
var replace = require('rollup-plugin-replace') | ||
var rollup = require('rollup'); | ||
// var buble = require('rollup-plugin-buble'); | ||
// var async = require('rollup-plugin-async') | ||
var replace = require('rollup-plugin-replace'); | ||
|
||
rollup | ||
.rollup({ | ||
input: 'packages/docsify-server-renderer/index.js', | ||
plugins: [ | ||
async(), | ||
// async(), | ||
replace({ | ||
__VERSION__: process.env.VERSION || require('../package.json').version, | ||
'process.env.SSR': true | ||
'process.env.SSR': true, | ||
}), | ||
buble({ | ||
transforms: { | ||
generator: false | ||
} | ||
}) | ||
// TODO restore this, for IE11. | ||
// buble({ | ||
// transforms: { | ||
// generator: false, | ||
// }, | ||
// }), | ||
], | ||
onwarn: function () {} | ||
onwarn: function() {}, | ||
}) | ||
.then(function (bundle) { | ||
var dest = 'packages/docsify-server-renderer/build.js' | ||
.then(function(bundle) { | ||
var dest = 'packages/docsify-server-renderer/build.js'; | ||
|
||
console.log(dest) | ||
console.log(dest); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. auto formatting based on the repo's eslint/prettier config files. |
||
return bundle.write({ | ||
format: 'cjs', | ||
file: dest | ||
}) | ||
}) | ||
.catch(function (err) { | ||
console.error(err) | ||
process.exit(1) | ||
file: dest, | ||
}); | ||
}) | ||
.catch(function(err) { | ||
console.error(err); | ||
process.exit(1); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ | |
"pub": "sh build/release.sh", | ||
"postinstall": "opencollective-postinstall" | ||
}, | ||
"husky": { | ||
"husky-OFF": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. temporary. if we accept any changes, they should all be formatted. Actually we should just go ahead and format the whole code base all in one PR and get it out of the way if we're going to have the eslint/prettier configs in the repo. |
||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>docsify</title> | ||
<meta | ||
name="viewport" | ||
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" | ||
/> | ||
<link rel="stylesheet" href="/themes/vue.css" title="vue" /> | ||
</head> | ||
<body> | ||
<!--inject-app--> | ||
<!--inject-config--> | ||
<script src="/lib/docsify.js"></script> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
const tmplPath = path.resolve(__dirname, 'default-template.html'); | ||
|
||
export function getDefaultTemplate() { | ||
return fs.readFileSync(tmplPath).toString(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,35 +9,29 @@ import { Compiler } from '../../src/core/render/compiler'; | |
import { isAbsolutePath } from '../../src/core/router/util'; | ||
import * as tpl from '../../src/core/render/tpl'; | ||
import { prerenderEmbed } from '../../src/core/render/embed'; | ||
import { getDefaultTemplate } from './default-template'; | ||
|
||
export { getDefaultTemplate }; | ||
|
||
function cwd(...args) { | ||
return resolve(process.cwd(), ...args); | ||
} | ||
|
||
// Borrowed from https://j11y.io/snippets/getting-a-fully-qualified-url. | ||
function qualifyURL(url) { | ||
// TODO this doesn't work in Node, passing in / results in /. It doesn't know the origin. Maybe we should update `location` globally first. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nevermind this comment. I fixed it by configuring JSDom. |
||
const img = document.createElement('img'); | ||
img.src = url; // set string url | ||
url = img.src; // get qualified url | ||
img.src = ''; // prevent the server request | ||
return url; | ||
} | ||
|
||
function isExternal(url) { | ||
let match = url.match( | ||
/^([^:\/?#]+:)?(?:\/\/([^\/?#]*))?([^?#]+)?(\?[^#]*)?(#.*)?/ | ||
); | ||
if ( | ||
typeof match[1] === 'string' && | ||
match[1].length > 0 && | ||
match[1].toLowerCase() !== location.protocol | ||
) { | ||
return true; | ||
} | ||
if ( | ||
typeof match[2] === 'string' && | ||
match[2].length > 0 && | ||
match[2].replace( | ||
new RegExp( | ||
':(' + { 'http:': 80, 'https:': 443 }[location.protocol] + ')?$' | ||
), | ||
'' | ||
) !== location.host | ||
) { | ||
return true; | ||
} | ||
return false; | ||
url = qualifyURL(url); | ||
// console.log('qualified URL:', url, location.origin); | ||
url = new URL(url); | ||
return url.origin !== location.origin; | ||
} | ||
|
||
function mainTpl(config) { | ||
|
@@ -59,12 +53,11 @@ function mainTpl(config) { | |
} | ||
|
||
export default class Renderer { | ||
constructor({ template, config, cache }) { | ||
constructor({ template, config }) { | ||
this.html = template; | ||
this.config = config = Object.assign({}, config, { | ||
routerMode: 'history', | ||
}); | ||
this.cache = cache; | ||
|
||
this.router = new AbstractHistory(config); | ||
this.compiler = new Compiler(config, this.router); | ||
|
@@ -208,4 +201,6 @@ export default class Renderer { | |
} | ||
} | ||
|
||
export { Renderer }; | ||
|
||
Renderer.version = '__VERSION__'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,25 @@ | ||
const liveServer = require('live-server') | ||
const isSSR = !!process.env.SSR | ||
const middleware = [] | ||
const liveServer = require('live-server'); | ||
const isSSR = !!process.env.SSR; | ||
const middleware = []; | ||
|
||
if (isSSR) { | ||
const Renderer = require('./packages/docsify-server-renderer/build.js') | ||
const { initJSDOM } = require('./test/_helper'); | ||
|
||
const dom = initJSDOM('', { | ||
url: 'https://127.0.0.1:3000', | ||
}); | ||
|
||
require = require('esm')(module /* , options */); | ||
|
||
const { | ||
Renderer, | ||
getDefaultTemplate, | ||
} = require('./packages/docsify-server-renderer/index'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Import the ES Modules. Much nicer experience in the debugger (Chrome devtools). Try it:
Once you run it, you should see an error inside the |
||
|
||
debugger; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. temporary. I will delete this commit and recreate the branch all cleaned up. We just need to decide what to do. |
||
|
||
const renderer = new Renderer({ | ||
template: ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>docsify</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0"> | ||
<link rel="stylesheet" href="/themes/vue.css" title="vue"> | ||
</head> | ||
<body> | ||
<!--inject-app--> | ||
<!--inject-config--> | ||
<script src="/lib/docsify.js"></script> | ||
</body> | ||
</html>`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved to a separate HTML file so it can be re-used (f.e. here, and in the test code). |
||
template: getDefaultTemplate(), | ||
config: { | ||
name: 'docsify', | ||
repo: 'docsifyjs/docsify', | ||
|
@@ -32,24 +32,24 @@ if (isSSR) { | |
'/de-de/changelog': '/changelog', | ||
'/zh-cn/changelog': '/changelog', | ||
'/changelog': | ||
'https://raw.githubusercontent.com/docsifyjs/docsify/master/CHANGELOG' | ||
} | ||
'https://raw.githubusercontent.com/docsifyjs/docsify/master/CHANGELOG', | ||
}, | ||
}, | ||
path: './' | ||
}) | ||
// path: './', // not used for anything? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused? The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I found out because I add Looks like we have some cleanup to do. |
||
}); | ||
|
||
middleware.push(function(req, res, next) { | ||
if (/\.(css|js)$/.test(req.url)) { | ||
return next() | ||
return next(); | ||
} | ||
renderer.renderToString(req.url).then(html => res.end(html)) | ||
}) | ||
renderer.renderToString(req.url).then(html => res.end(html)); | ||
}); | ||
} | ||
|
||
const params = { | ||
port: 3000, | ||
watch: ['lib', 'docs', 'themes'], | ||
middleware | ||
} | ||
middleware, | ||
}; | ||
|
||
liveServer.start(params) | ||
liveServer.start(params); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// @ts-check | ||
/* eslint-disable no-global-assign */ | ||
require = require('esm')( | ||
module /* , options */ | ||
); /* eslint-disable-line no-global-assign */ | ||
const { expect } = require('chai'); | ||
const { initJSDOM } = require('../_helper'); | ||
|
||
// const port = 9754; | ||
// const docsifySite = 'http://127.0.0.1:' + port; | ||
|
||
initJSDOM(); | ||
|
||
const { | ||
Renderer, | ||
getDefaultTemplate, | ||
} = require('../../packages/docsify-server-renderer/index'); | ||
|
||
describe('pacakges/docsify-server-render', function() { | ||
it('renders content', async function() { | ||
const renderer = new Renderer({ | ||
template: getDefaultTemplate(), | ||
config: { | ||
name: 'docsify', | ||
repo: 'docsifyjs/docsify', | ||
// basePath: 'https://docsify.js.org/', | ||
loadNavbar: true, | ||
loadSidebar: true, | ||
subMaxLevel: 3, | ||
auto2top: true, | ||
alias: { | ||
'/de-de/changelog': '/changelog', | ||
'/zh-cn/changelog': '/changelog', | ||
'/changelog': | ||
'https://raw.githubusercontent.com/docsifyjs/docsify/master/CHANGELOG', | ||
}, | ||
}, | ||
}); | ||
|
||
await renderer.renderToString('/changelog'); | ||
|
||
expect(renderer).to.be.an.instanceof(Renderer); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A starting point to officially test the server code. |
||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The output is nice without this. async functions and generators work great in newer Node.