Skip to content

Commit 023e3bb

Browse files
committed
Refactor footnote handling and update build process
1 parent ea3e22e commit 023e3bb

File tree

4 files changed

+58
-106
lines changed

4 files changed

+58
-106
lines changed

.github/workflows/publish.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99

1010
jobs:
1111
publish:
12+
if: github.repository == 'sy-records/docsify-footnotes'
1213
runs-on: ubuntu-latest
1314

1415
steps:

build/build.js

Lines changed: 45 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,54 @@
1-
const rollup = require('rollup')
2-
const buble = require('rollup-plugin-buble')
3-
const commonjs = require('rollup-plugin-commonjs')
4-
const nodeResolve = require('rollup-plugin-node-resolve')
5-
const terser = require('@rollup/plugin-terser')
6-
const replace = require('rollup-plugin-replace')
7-
const isProd = process.env.NODE_ENV === 'production'
8-
const version = process.env.VERSION || require('../package.json').version
1+
const fs = require('fs');
2+
const path = require('path');
3+
const { minify } = require('terser');
4+
5+
const rootDir = path.resolve(__dirname, '..');
6+
const inputFile = path.join(rootDir, 'src', 'index.js');
7+
const distDir = path.join(rootDir, 'dist');
8+
const outputFile = path.join(distDir, 'index.js');
9+
const outputMinFile = path.join(distDir, 'index.min.js');
10+
const outputMinMapFile = path.join(distDir, 'index.min.js.map');
11+
12+
function cleanDist() {
13+
if (fs.existsSync(distDir)) {
14+
fs.rmSync(distDir, { recursive: true, force: true });
15+
console.log('🧹 Cleaned up dist directory');
16+
}
17+
fs.mkdirSync(distDir, { recursive: true });
18+
}
919

10-
/**
11-
* @param {{
12-
* input: string,
13-
* output?: string,
14-
* globalName?: string,
15-
* plugins?: Array<import('rollup').Plugin>
16-
* }} opts
17-
*/
18-
async function build(opts) {
19-
await rollup
20-
.rollup({
21-
input: opts.input,
22-
plugins: (opts.plugins || []).concat([
23-
buble(),
24-
commonjs(),
25-
nodeResolve(),
26-
replace({
27-
__VERSION__: version
28-
})
29-
]),
30-
onwarn: function (message) {
31-
if (message.code === 'UNRESOLVED_IMPORT') {
32-
throw new Error(
33-
`Could not resolve module ` +
34-
message.source +
35-
`. Try running 'npm install' or using rollup's 'external' option if this is an external dependency. ` +
36-
`Module ${message.source} is imported in ${message.importer}`
37-
)
38-
}
39-
}
40-
})
41-
.then(function (bundle) {
42-
var dest = 'dist/' + (opts.output || opts.input)
20+
async function build() {
21+
cleanDist();
4322

44-
console.log(dest)
45-
return bundle.write({
46-
output: {file: dest, format: 'iife', strict: false},
47-
})
48-
})
49-
}
23+
if (!fs.existsSync(inputFile)) {
24+
console.error(`❌ Source file not found: ${inputFile}`);
25+
return;
26+
}
5027

51-
async function buildPlugin() {
52-
var plugins = [
53-
{name: 'index', input: 'index.js'},
54-
]
28+
let sourceCode = fs.readFileSync(inputFile, 'utf-8');
5529

56-
const promises = plugins.map(item => {
57-
return build({
58-
input: 'src/' + item.input,
59-
output: item.name + '.js'
60-
})
61-
})
30+
const wrappedCode = `(function () {\n${sourceCode}\n})();`;
31+
fs.writeFileSync(outputFile, wrappedCode, 'utf-8');
32+
console.log('✅ Generated non-minified version (IIFE): dist/index.js');
6233

63-
if (isProd) {
64-
plugins.forEach(item => {
65-
promises.push(build({
66-
input: 'src/' + item.input,
67-
output: item.name + '.min.js',
68-
plugins: [terser()]
69-
}))
70-
})
71-
}
34+
try {
35+
const minified = await minify(sourceCode, {
36+
compress: true,
37+
mangle: true,
38+
sourceMap: {
39+
filename: 'index.min.js',
40+
url: 'index.min.js.map'
41+
}
42+
});
7243

73-
await Promise.all(promises)
74-
}
44+
fs.writeFileSync(outputMinFile, minified.code, 'utf-8');
45+
console.log('✅ Generated minified version: dist/index.min.js');
7546

76-
async function main() {
77-
await Promise.all([
78-
buildPlugin()
79-
])
47+
fs.writeFileSync(outputMinMapFile, minified.map, 'utf-8');
48+
console.log('✅ Generated minified source map: dist/index.min.js.map');
49+
} catch (err) {
50+
console.error('❌ Error during minification:', err);
51+
}
8052
}
8153

82-
main().catch((e) => {
83-
console.error(e)
84-
process.exit(1)
85-
})
54+
build();

package.json

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sy-records/docsify-footnotes",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "A plugin that supports the use of footnotes in docsify.",
55
"main": "dist/index.js",
66
"repository": {
@@ -19,16 +19,9 @@
1919
},
2020
"homepage": "https://github.com/sy-records/docsify-footnotes#readme",
2121
"devDependencies": {
22-
"@rollup/plugin-terser": "^0.4.4",
23-
"cross-env": "^6.0.3",
24-
"rollup": "^2.79.2",
25-
"rollup-plugin-async": "^1.2.0",
26-
"rollup-plugin-buble": "^0.19.8",
27-
"rollup-plugin-commonjs": "^10.1.0",
28-
"rollup-plugin-node-resolve": "^5.2.0",
29-
"rollup-plugin-replace": "^2.2.0"
22+
"terser": "^5.37.0"
3023
},
3124
"scripts": {
32-
"build": "cross-env NODE_ENV=production node build/build.js"
25+
"build": "node build/build.js"
3326
}
3427
}

src/index.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
11
document.addEventListener('click', function (event) {
22
const target = event.target.closest('.footnote-symbol, .footnote-reference-symbol');
3+
if (!target?.dataset.ref) return;
34

4-
if (!target || !target.hasAttribute('data-id')) return;
5-
6-
const dataId = target.getAttribute('data-id');
7-
8-
const footnotesElm = Docsify.dom.find(`.markdown-section :where(sup, strong)[id="${dataId}"]`);
9-
10-
if (footnotesElm) {
11-
footnotesElm.scrollIntoView({
12-
behavior: 'smooth',
13-
block: 'start',
14-
});
15-
}
5+
const footnotesElm = Docsify.dom.find(`.markdown-section :where(.footnote-symbol, .footnote-reference-symbol)[id="${target.dataset.ref}"]`);
6+
footnotesElm?.scrollIntoView({ behavior: 'smooth', block: 'start' });
167
});
178

189
function footnotes(hook) {
19-
hook.beforeEach(function (markdown) {
20-
if (/\[\^([A-Za-z0-9\-]+)\][^:]/.test(markdown)) {
21-
markdown = markdown
22-
.replace(/\[\^([A-Za-z0-9\-]+)\][^:]/gm, '<sup class="footnote-symbol" data-id="fnref-$1" id="fn-$1">[\[$1]\](#fnref-$1)</sup>')
23-
.replace(/\[\^([A-Za-z0-9\-]+)\]\: /gm, '<strong class="footnote-reference-symbol" data-id="fn-$1" id="fnref-$1">[\[$1\]](#fn-$1)</strong>:leftwards_arrow_with_hook: ');
24-
}
25-
26-
return markdown;
10+
hook.beforeEach((markdown) => {
11+
return markdown.replace(/\[\^([A-Za-z0-9\-]+)\](\:)?/gm, (_, id, isDefinition) =>
12+
isDefinition
13+
? `<strong class="footnote-reference-symbol" data-ref="fn-${id}" id="fnref-${id}">[${id}](#fn-${id})</strong>:leftwards_arrow_with_hook: `
14+
: `<sup class="footnote-symbol" data-ref="fnref-${id}" id="fn-${id}">[${id}](#fnref-${id})</sup>`
15+
);
2716
});
2817
}
2918

0 commit comments

Comments
 (0)