diff --git a/src/js/build-html.js b/src/js/build-html.js index 7ff75dac..5f0a7b65 100644 --- a/src/js/build-html.js +++ b/src/js/build-html.js @@ -9,7 +9,8 @@ export default function (options) { var forEach = [].forEach var some = [].some - var body = document.body + // if (typeof window === 'undefined') return + var body = typeof window !== 'undefined' && document.body var tocElement var currentlyHighlighting = true var SPACE_CHAR = ' ' diff --git a/src/js/scroll-smooth/index.js b/src/js/scroll-smooth/index.js index 652f0bd1..7e1f4a6e 100644 --- a/src/js/scroll-smooth/index.js +++ b/src/js/scroll-smooth/index.js @@ -6,6 +6,7 @@ export default function initSmoothScrolling (options) { var duration = options.duration var offset = options.offset + if (typeof window === 'undefined' || typeof location === 'undefined') return var pageUrl = location.hash ? stripHash(location.href) diff --git a/src/js/server-render.js b/src/js/server-render.js new file mode 100644 index 00000000..3813ef54 --- /dev/null +++ b/src/js/server-render.js @@ -0,0 +1,29 @@ +import * as tocbot from './index-esm.js' +import { JSDOM } from 'jsdom' + +export function htmlTemplate (content){ + return ` + + +
+ ${content} +
+
+
+ + +` +} + +export function serverRender (content) { + const html = htmlTemplate(content) + const { window, location } = new JSDOM(html) + global.window = window + global.document = window.document + global.location = location + + // Init and get HTML content. + tocbot.init() + const toc = window.document.body.querySelector('.js-toc') + return toc && toc.innerHTML +}