Skip to content
Closed
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
13 changes: 6 additions & 7 deletions asset/js/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function setupAnchors() {
top: calc(-${headerHeight}px - ${bufferHeight}rem)
}`,
);
insertCss(`span.card-container::before {
display: block;
content: '';
margin-top: calc(-${headerHeight}px - ${bufferHeight}rem);
height: calc(${headerHeight}px + ${bufferHeight}rem);
}`);
}
jQuery('h1, h2, h3, h4, h5, h6, .header-wrapper').each((index, heading) => {
if (heading.id) {
Expand All @@ -48,13 +54,6 @@ function setupAnchors() {
jQuery(heading).on('mouseleave',
() => jQuery(heading).find('.fa.fa-anchor').css('visibility', 'hidden'));
if (isFixed) {
/**
* Fixing the top navbar would break anchor navigation,
* by creating empty spans above the <h> tag we can prevent
* the headings from being covered by the navbar.
*/
const spanId = heading.id;
heading.insertAdjacentHTML('beforebegin', `<span id="${spanId}" class="anchor"></span>`);
jQuery(heading).removeAttr('id'); // to avoid duplicated id problem
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -1172,9 +1172,35 @@ class Page {
this.pluginsContext[pluginName] || {}, this.frontMatter, this.getPluginConfig());
}
});
// After rendering everything, add dummy spans to headings if a fixed header is present.
postRenderedContent = Page._postRenderForFixedHeader(postRenderedContent);
return postRenderedContent;
}

/**
* Generate dummy spans if fixed header is used.
* @param content HTML content to be rendered.
* @return {string} Rendered HTML content.
* @private
*/
static _postRenderForFixedHeader(content) {
const $ = cheerio.load(content, { xmlMode: false });
const hasHeader = $('header')[0] !== undefined;
if (hasHeader) {
// Check if the first header has a class named 'header-fixed'
const isFixed = $('header')[0].attribs === undefined
? false : $('header')[0].attribs.class === 'header-fixed';
if (isFixed) {
$('h1, h2, h3, h4, h5, h6, .header-wrapper').each((index, heading) => {
const spanId = heading.attribs.id;
$(heading).prepend(`<span id="${spanId}" class="anchor"></span>`);
});
}
return $.html();
}
return content;
}

/**
* Collect page content inserted by plugins
*/
Expand Down
13 changes: 6 additions & 7 deletions test/functional/test_site/expected/markbind/js/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function setupAnchors() {
top: calc(-${headerHeight}px - ${bufferHeight}rem)
}`,
);
insertCss(`span.card-container::before {
display: block;
content: '';
margin-top: calc(-${headerHeight}px - ${bufferHeight}rem);
height: calc(${headerHeight}px + ${bufferHeight}rem);
}`);
}
jQuery('h1, h2, h3, h4, h5, h6, .header-wrapper').each((index, heading) => {
if (heading.id) {
Expand All @@ -48,13 +54,6 @@ function setupAnchors() {
jQuery(heading).on('mouseleave',
() => jQuery(heading).find('.fa.fa-anchor').css('visibility', 'hidden'));
if (isFixed) {
/**
* Fixing the top navbar would break anchor navigation,
* by creating empty spans above the <h> tag we can prevent
* the headings from being covered by the navbar.
*/
const spanId = heading.id;
heading.insertAdjacentHTML('beforebegin', `<span id="${spanId}" class="anchor"></span>`);
jQuery(heading).removeAttr('id'); // to avoid duplicated id problem
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function setupAnchors() {
top: calc(-${headerHeight}px - ${bufferHeight}rem)
}`,
);
insertCss(`span.card-container::before {
display: block;
content: '';
margin-top: calc(-${headerHeight}px - ${bufferHeight}rem);
height: calc(${headerHeight}px + ${bufferHeight}rem);
}`);
}
jQuery('h1, h2, h3, h4, h5, h6, .header-wrapper').each((index, heading) => {
if (heading.id) {
Expand All @@ -48,13 +54,6 @@ function setupAnchors() {
jQuery(heading).on('mouseleave',
() => jQuery(heading).find('.fa.fa-anchor').css('visibility', 'hidden'));
if (isFixed) {
/**
* Fixing the top navbar would break anchor navigation,
* by creating empty spans above the <h> tag we can prevent
* the headings from being covered by the navbar.
*/
const spanId = heading.id;
heading.insertAdjacentHTML('beforebegin', `<span id="${spanId}" class="anchor"></span>`);
jQuery(heading).removeAttr('id'); // to avoid duplicated id problem
}
}
Expand Down
13 changes: 6 additions & 7 deletions test/functional/test_site_convert/expected/markbind/js/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function setupAnchors() {
top: calc(-${headerHeight}px - ${bufferHeight}rem)
}`,
);
insertCss(`span.card-container::before {
display: block;
content: '';
margin-top: calc(-${headerHeight}px - ${bufferHeight}rem);
height: calc(${headerHeight}px + ${bufferHeight}rem);
}`);
}
jQuery('h1, h2, h3, h4, h5, h6, .header-wrapper').each((index, heading) => {
if (heading.id) {
Expand All @@ -48,13 +54,6 @@ function setupAnchors() {
jQuery(heading).on('mouseleave',
() => jQuery(heading).find('.fa.fa-anchor').css('visibility', 'hidden'));
if (isFixed) {
/**
* Fixing the top navbar would break anchor navigation,
* by creating empty spans above the <h> tag we can prevent
* the headings from being covered by the navbar.
*/
const spanId = heading.id;
heading.insertAdjacentHTML('beforebegin', `<span id="${spanId}" class="anchor"></span>`);
jQuery(heading).removeAttr('id'); // to avoid duplicated id problem
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function setupAnchors() {
top: calc(-${headerHeight}px - ${bufferHeight}rem)
}`,
);
insertCss(`span.card-container::before {
display: block;
content: '';
margin-top: calc(-${headerHeight}px - ${bufferHeight}rem);
height: calc(${headerHeight}px + ${bufferHeight}rem);
}`);
}
jQuery('h1, h2, h3, h4, h5, h6, .header-wrapper').each((index, heading) => {
if (heading.id) {
Expand All @@ -48,13 +54,6 @@ function setupAnchors() {
jQuery(heading).on('mouseleave',
() => jQuery(heading).find('.fa.fa-anchor').css('visibility', 'hidden'));
if (isFixed) {
/**
* Fixing the top navbar would break anchor navigation,
* by creating empty spans above the <h> tag we can prevent
* the headings from being covered by the navbar.
*/
const spanId = heading.id;
heading.insertAdjacentHTML('beforebegin', `<span id="${spanId}" class="anchor"></span>`);
jQuery(heading).removeAttr('id'); // to avoid duplicated id problem
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function setupAnchors() {
top: calc(-${headerHeight}px - ${bufferHeight}rem)
}`,
);
insertCss(`span.card-container::before {
display: block;
content: '';
margin-top: calc(-${headerHeight}px - ${bufferHeight}rem);
height: calc(${headerHeight}px + ${bufferHeight}rem);
}`);
}
jQuery('h1, h2, h3, h4, h5, h6, .header-wrapper').each((index, heading) => {
if (heading.id) {
Expand All @@ -48,13 +54,6 @@ function setupAnchors() {
jQuery(heading).on('mouseleave',
() => jQuery(heading).find('.fa.fa-anchor').css('visibility', 'hidden'));
if (isFixed) {
/**
* Fixing the top navbar would break anchor navigation,
* by creating empty spans above the <h> tag we can prevent
* the headings from being covered by the navbar.
*/
const spanId = heading.id;
heading.insertAdjacentHTML('beforebegin', `<span id="${spanId}" class="anchor"></span>`);
jQuery(heading).removeAttr('id'); // to avoid duplicated id problem
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function setupAnchors() {
top: calc(-${headerHeight}px - ${bufferHeight}rem)
}`,
);
insertCss(`span.card-container::before {
display: block;
content: '';
margin-top: calc(-${headerHeight}px - ${bufferHeight}rem);
height: calc(${headerHeight}px + ${bufferHeight}rem);
}`);
}
jQuery('h1, h2, h3, h4, h5, h6, .header-wrapper').each((index, heading) => {
if (heading.id) {
Expand All @@ -48,13 +54,6 @@ function setupAnchors() {
jQuery(heading).on('mouseleave',
() => jQuery(heading).find('.fa.fa-anchor').css('visibility', 'hidden'));
if (isFixed) {
/**
* Fixing the top navbar would break anchor navigation,
* by creating empty spans above the <h> tag we can prevent
* the headings from being covered by the navbar.
*/
const spanId = heading.id;
heading.insertAdjacentHTML('beforebegin', `<span id="${spanId}" class="anchor"></span>`);
jQuery(heading).removeAttr('id'); // to avoid duplicated id problem
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function setupAnchors() {
top: calc(-${headerHeight}px - ${bufferHeight}rem)
}`,
);
insertCss(`span.card-container::before {
display: block;
content: '';
margin-top: calc(-${headerHeight}px - ${bufferHeight}rem);
height: calc(${headerHeight}px + ${bufferHeight}rem);
}`);
}
jQuery('h1, h2, h3, h4, h5, h6, .header-wrapper').each((index, heading) => {
if (heading.id) {
Expand All @@ -48,13 +54,6 @@ function setupAnchors() {
jQuery(heading).on('mouseleave',
() => jQuery(heading).find('.fa.fa-anchor').css('visibility', 'hidden'));
if (isFixed) {
/**
* Fixing the top navbar would break anchor navigation,
* by creating empty spans above the <h> tag we can prevent
* the headings from being covered by the navbar.
*/
const spanId = heading.id;
heading.insertAdjacentHTML('beforebegin', `<span id="${spanId}" class="anchor"></span>`);
jQuery(heading).removeAttr('id'); // to avoid duplicated id problem
}
}
Expand Down