Skip to content

Commit b53ea1e

Browse files
authored
Adds option to specify an header height (#1045)
* Adds option to specify an header height When using a template that has a sticky-header, clicking on the sidebar will scroll the page under the header. I added the option `headerHeight` (default = `0`) so that the content div will be scrolled down that amount of pixels. * updates documentation and renames variable
1 parent 562cd7c commit b53ea1e

File tree

11 files changed

+32
-17
lines changed

11 files changed

+32
-17
lines changed

docs/configuration.md

+13
Original file line numberDiff line numberDiff line change
@@ -575,3 +575,16 @@ window.$docsify = {
575575
```
576576

577577
> Note: The options with fallbackLanguages didn't work with the `notFoundPage` options.
578+
579+
## topMargin
580+
581+
- type: `Number`
582+
- default: `0`
583+
584+
Adds a space on top when scrolling content page to reach the selected section. This is useful in case you have a _sticky-header_ layout and you want to align anchors to the end of your header.
585+
586+
```js
587+
window.$docsify = {
588+
topMargin: 90, // default: 0
589+
};
590+
```

packages/docsify-server-renderer/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { readFileSync } from 'fs';
22
import { resolve, basename } from 'path';
3+
import resolvePathname from 'resolve-pathname';
4+
import debug from 'debug';
5+
import fetch from 'node-fetch';
36
import { AbstractHistory } from '../../src/core/router/history/abstract';
47
import { Compiler } from '../../src/core/render/compiler';
58
import { isAbsolutePath } from '../../src/core/router/util';
69
import * as tpl from '../../src/core/render/tpl';
710
import { prerenderEmbed } from '../../src/core/render/embed';
8-
import resolvePathname from 'resolve-pathname';
9-
import debug from 'debug';
10-
import fetch from 'node-fetch';
1111

1212
function cwd(...args) {
1313
return resolve(process.cwd(), ...args);

src/core/config.js

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export default function() {
3333
routerMode: 'hash',
3434
noCompileLinks: [],
3535
relativePath: false,
36+
topMargin: 0,
3637
},
3738
window.$docsify
3839
);

src/core/event/scroll.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
import { isMobile } from '../util/env';
2-
import * as dom from '../util/dom';
31
import Tweezer from 'tweezer.js';
42
import cssEscape from 'css.escape';
3+
import { isMobile } from '../util/env';
4+
import * as dom from '../util/dom';
5+
import config from '../config';
56

67
const nav = {};
78
let hoverOver = false;
89
let scroller = null;
910
let enableScrollEvent = true;
1011
let coverHeight = 0;
1112

12-
function scrollTo(el) {
13+
function scrollTo(el, offset = 0) {
1314
if (scroller) {
1415
scroller.stop();
1516
}
1617

1718
enableScrollEvent = false;
1819
scroller = new Tweezer({
1920
start: window.pageYOffset,
20-
end: el.getBoundingClientRect().top + window.pageYOffset,
21+
end: el.getBoundingClientRect().top + window.pageYOffset - offset,
2122
duration: 500,
2223
})
2324
.on('tick', v => window.scrollTo(0, v))
@@ -142,9 +143,9 @@ export function scrollIntoView(path, id) {
142143
if (!id) {
143144
return;
144145
}
145-
146+
const topMargin = config().topMargin;
146147
const section = dom.find('#' + cssEscape(id));
147-
section && scrollTo(section);
148+
section && scrollTo(section, topMargin);
148149

149150
const li = nav[getNavKey(path, id)];
150151
const sidebar = dom.getNode('.sidebar');

src/core/global-api.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import marked from 'marked';
2+
import prism from 'prismjs';
13
import * as util from './util';
24
import * as dom from './util/dom';
35
import { Compiler } from './render/compiler';
46
import { slugify } from './render/slugify';
57
import { get } from './fetch/ajax';
6-
import marked from 'marked';
7-
import prism from 'prismjs';
88

99
export default function() {
1010
window.Docsify = {

src/core/render/compiler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import marked from 'marked';
12
import { isAbsolutePath, getPath, getParentPath } from '../router/util';
23
import { isFn, merge, cached, isPrimitive } from '../util/core';
34
import { tree as treeTpl } from './tpl';
@@ -10,7 +11,6 @@ import { paragraphCompiler } from './compiler/paragraph';
1011
import { taskListCompiler } from './compiler/taskList';
1112
import { taskListItemCompiler } from './compiler/taskListItem';
1213
import { linkCompiler } from './compiler/link';
13-
import marked from 'marked';
1414

1515
const cachedLinks = {};
1616

src/core/render/embed.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import stripIndent from 'strip-indent';
12
import { get } from '../fetch/ajax';
23
import { merge } from '../util/core';
3-
import stripIndent from 'strip-indent';
44

55
const cached = {};
66

src/core/render/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable no-unused-vars */
2+
import tinydate from 'tinydate';
23
import * as dom from '../util/dom';
34
import cssVars from '../util/polyfill/css-vars';
45
import { callHook } from '../init/lifecycle';
@@ -10,7 +11,6 @@ import { scrollActiveSidebar, scroll2Top } from '../event/scroll';
1011
import { Compiler } from './compiler';
1112
import * as tpl from './tpl';
1213
import { prerenderEmbed } from './embed';
13-
import tinydate from 'tinydate';
1414

1515
function executeScript() {
1616
const script = dom

test/unit/base.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
require = require('esm')(
33
module /* , options */
44
); /* eslint-disable-line no-global-assign */
5-
const { History } = require('../../src/core/router/history/base');
65
const { expect } = require('chai');
6+
const { History } = require('../../src/core/router/history/base');
77

88
class MockHistory extends History {
99
parse(path) {

test/unit/render.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { init, expectSameDom } = require('../_helper');
21
const { expect } = require('chai');
2+
const { init, expectSameDom } = require('../_helper');
33

44
describe('render', function() {
55
it('important content (tips)', async function() {

test/unit/util.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
require = require('esm')(
33
module /* , options */
44
); /* eslint-disable-line no-global-assign */
5-
const { resolvePath } = require('../../src/core/router/util');
65
const { expect } = require('chai');
6+
const { resolvePath } = require('../../src/core/router/util');
77

88
describe('router/util', function() {
99
it('resolvePath', async function() {

0 commit comments

Comments
 (0)