diff --git a/asset/css/markbind.css b/asset/css/markbind.css index e21c7c4808..24462b16fa 100644 --- a/asset/css/markbind.css +++ b/asset/css/markbind.css @@ -73,20 +73,6 @@ kbd { outline: none !important; } -.fa.fa-anchor { - color: #ccc; - display: inline; - font-size: 14px; - margin-left: 10px; - padding: 3px; - text-decoration: none; - visibility: hidden; -} - -.fa.fa-anchor:hover { - color: #555; -} - code.hljs.inline { background: #f8f8f8; color: #333; diff --git a/asset/js/setup.js b/asset/js/setup.js index c6b54ef24f..d7910b2eb3 100644 --- a/asset/js/setup.js +++ b/asset/js/setup.js @@ -13,63 +13,47 @@ function scrollToUrlAnchorHeading() { } } -function flattenModals() { - jQuery('.modal').each((index, modal) => { - jQuery(modal).detach().appendTo(jQuery('#app')); - }); -} - function insertCss(cssCode) { const newNode = document.createElement('style'); newNode.innerHTML = cssCode; document.getElementsByTagName('head')[0].appendChild(newNode); } -function setupAnchors() { +function setupAnchorsForFixedNavbar() { const headerSelector = jQuery('header'); const isFixed = headerSelector.filter('.header-fixed').length !== 0; + if (!isFixed) { + return; + } + const headerHeight = headerSelector.height(); const bufferHeight = 1; - if (isFixed) { - jQuery('.nav-inner').css('padding-top', `calc(${headerHeight}px)`); - jQuery('#content-wrapper').css('padding-top', `calc(${headerHeight}px)`); - insertCss( - `span.anchor { - display: block; - position: relative; - top: calc(-${headerHeight}px - ${bufferHeight}rem) - }`, - ); - } + jQuery('.nav-inner').css('padding-top', `calc(${headerHeight}px)`); + jQuery('#content-wrapper').css('padding-top', `calc(${headerHeight}px)`); + insertCss( + `span.anchor { + display: block; + position: relative; + top: calc(-${headerHeight}px - ${bufferHeight}rem) + }`, + ); jQuery('h1, h2, h3, h4, h5, h6, .header-wrapper').each((index, heading) => { if (heading.id) { - jQuery(heading).on('mouseenter', - () => jQuery(heading).find('.fa.fa-anchor').css('visibility', 'visible')); - 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 tag we can prevent - * the headings from being covered by the navbar. - */ - const spanId = heading.id; - heading.insertAdjacentHTML('beforebegin', ``); - jQuery(heading).removeAttr('id'); // to avoid duplicated id problem - } + /** + * Fixing the top navbar would break anchor navigation, + * by creating empty spans above the tag we can prevent + * the headings from being covered by the navbar. + */ + const spanId = heading.id; + heading.insertAdjacentHTML('beforebegin', ``); + jQuery(heading).removeAttr('id'); // to avoid duplicated id problem } }); - jQuery('.fa-anchor').each((index, anchor) => { - jQuery(anchor).on('click', function () { - window.location.href = jQuery(this).attr('href'); - }); - }); } function updateSearchData(vm) { jQuery.getJSON(`${baseUrl}/siteData.json`) .then((siteData) => { - // eslint-disable-next-line no-param-reassign vm.searchData = siteData.pages; }); } @@ -97,9 +81,8 @@ function executeAfterCreatedRoutines() { } function executeAfterMountedRoutines() { - flattenModals(); scrollToUrlAnchorHeading(); - setupAnchors(); + setupAnchorsForFixedNavbar(); MarkBind.executeAfterSetupScripts.resolve(); } diff --git a/docs/userGuide/usingPlugins.md b/docs/userGuide/usingPlugins.md index e154828d93..a825b84c65 100644 --- a/docs/userGuide/usingPlugins.md +++ b/docs/userGuide/usingPlugins.md @@ -117,14 +117,21 @@ Plugins can implement the methods `getLinks` and `getScripts` to add additional - `frontMatter`: The frontMatter of the page being processed, in case any frontMatter data is required. - `utils`: Object containing the following utility functions - `buildStylesheet(href)`: Builds a stylesheet link element with the specified `href`. - - Should return an array of string data containing link elements to be added. + - Should return an array of strings containing link elements to be added. - `getScripts(content, pluginContext, frontMatter, utils)`: Called to get script elements to be added after the body of the page. - `content`: The rendered HTML. - `pluginContext`: User provided parameters for the plugin. This can be specified in the `site.json`. - `frontMatter`: The frontMatter of the page being processed, in case any frontMatter data is required. - `utils`: Object containing the following utility functions - `buildScript(src)`: Builds a script element with the specified `src`. - - Should return an array of string data containing script elements to be added. + - Should return an array of strings containing script elements to be added. + + + +You can set an absolute or relative file path as the `src` or `href` attribute in your `'], + getLinks: (content, pluginContext, frontMatter, utils) => [utils.buildStylesheet(TEST_STYLESHEET_FILE)], + getScripts: (content, pluginContext, frontMatter, utils) => [ + // Explicitly resolve to test absolute file paths + utils.buildScript(path.resolve(__dirname, TEST_SCRIPT_FILE)), + '', + ], }; diff --git a/test/functional/test_site/_markbind/plugins/testMarkbindPluginScript.js b/test/functional/test_site/_markbind/plugins/testMarkbindPluginScript.js new file mode 100644 index 0000000000..06af89334a --- /dev/null +++ b/test/functional/test_site/_markbind/plugins/testMarkbindPluginScript.js @@ -0,0 +1,2 @@ +// eslint-disable-next-line no-alert +alert('External plugin script file loaded!'); diff --git a/test/functional/test_site/_markbind/plugins/testMarkbindPluginStylesheet.css b/test/functional/test_site/_markbind/plugins/testMarkbindPluginStylesheet.css new file mode 100644 index 0000000000..6d91334c05 --- /dev/null +++ b/test/functional/test_site/_markbind/plugins/testMarkbindPluginStylesheet.css @@ -0,0 +1,3 @@ +strong { + color: #138bf0; +} diff --git a/test/functional/test_site/expected/_markbind/plugins/testMarkbindPlugin.js b/test/functional/test_site/expected/_markbind/plugins/testMarkbindPlugin.js index e696ada3a8..5b2af6261a 100644 --- a/test/functional/test_site/expected/_markbind/plugins/testMarkbindPlugin.js +++ b/test/functional/test_site/expected/_markbind/plugins/testMarkbindPlugin.js @@ -1,4 +1,8 @@ const cheerio = module.parent.require('cheerio'); +const path = require('path'); + +const TEST_STYLESHEET_FILE = 'testMarkbindPluginStylesheet.css'; +const TEST_SCRIPT_FILE = 'testMarkbindPluginScript.js'; module.exports = { preRender: (content, pluginContext) => @@ -8,7 +12,10 @@ module.exports = { $('#test-markbind-plugin').append(`${pluginContext.post}`); return $.html(); }, - getLinks: (content, pluginContext, frontMatter, utils) => [utils.buildStylesheet('STYLESHEET_LINK')], - getScripts: (content, pluginContext, frontMatter, utils) => - [utils.buildScript('SCRIPT_LINK'), ''], + getLinks: (content, pluginContext, frontMatter, utils) => [utils.buildStylesheet(TEST_STYLESHEET_FILE)], + getScripts: (content, pluginContext, frontMatter, utils) => [ + // Explicitly resolve to test absolute file paths + utils.buildScript(path.resolve(__dirname, TEST_SCRIPT_FILE)), + '', + ], }; diff --git a/test/functional/test_site/expected/_markbind/plugins/testMarkbindPluginScript.js b/test/functional/test_site/expected/_markbind/plugins/testMarkbindPluginScript.js new file mode 100644 index 0000000000..06af89334a --- /dev/null +++ b/test/functional/test_site/expected/_markbind/plugins/testMarkbindPluginScript.js @@ -0,0 +1,2 @@ +// eslint-disable-next-line no-alert +alert('External plugin script file loaded!'); diff --git a/test/functional/test_site/expected/_markbind/plugins/testMarkbindPluginStylesheet.css b/test/functional/test_site/expected/_markbind/plugins/testMarkbindPluginStylesheet.css new file mode 100644 index 0000000000..6d91334c05 --- /dev/null +++ b/test/functional/test_site/expected/_markbind/plugins/testMarkbindPluginStylesheet.css @@ -0,0 +1,3 @@ +strong { + color: #138bf0; +} diff --git a/test/functional/test_site/expected/bugs/index.html b/test/functional/test_site/expected/bugs/index.html index 80288776b9..02e6f1500b 100644 --- a/test/functional/test_site/expected/bugs/index.html +++ b/test/functional/test_site/expected/bugs/index.html @@ -15,7 +15,8 @@ - + + @@ -63,9 +64,9 @@ const enableSearch = true - + diff --git a/test/functional/test_site/expected/index.html b/test/functional/test_site/expected/index.html index b0e4daac7d..cd35b57563 100644 --- a/test/functional/test_site/expected/index.html +++ b/test/functional/test_site/expected/index.html @@ -20,7 +20,8 @@ - + + @@ -49,12 +50,12 @@