Skip to content

Commit

Permalink
Fix: translator - gulpfile.js (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmosHuKe authored Aug 7, 2024
1 parent 7ea348f commit 51b850a
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions tool/translator/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ const replace = require('gulp-replace');
// 为了翻译工具能正确匹配,
// 需要为一些结构格式做出调整。

// 适用路径
const gulpSrc = [
'../../_site/**/!(*_cn).html',
];

// 去掉中文标点与中文间的空格
gulp.task('remove-space', () => {
return gulp.src(['../../_site/**/*.html'])
Expand All @@ -18,7 +23,7 @@ gulp.task('mark-side-toc', () => {
// 移动端显示屏顶部目录 正则匹配
const mobileRegexp = /<li class="toc-entry">\s*<a href="#(.*?)">(?!.*[\u4e00-\u9fa5])(.*?)<\/a>\s*<\/li>\s*<li class="toc-entry">\s*<a href="#(.*?)">(?=.*[\u4e00-\u9fa5])(.*?)<\/a>/g;

return gulp.src(['../../_site/**/*.html'])
return gulp.src(gulpSrc)
// 宽显示屏右侧目录
.pipe(
replace(wideRegexp, (match, p1, p2, p3, p4) => {
Expand All @@ -39,29 +44,29 @@ gulp.task('mark-side-toc', () => {
// 匹配替换正文分级标题相关格式
gulp.task('mark-side-level-title', () => {
// 正文分级标题 正则匹配
const titleRegexp = /<div class="header-wrapper">\s*<h(\d) id="(.*?)">(?!.*[\u4e00-\u9fa5])(.*?)<\/h(\d)>\s*<a class="heading-link" href="#(.*?)" aria-label="(.*?)">#<\/a>\s*<\/div>\s*<div class="header-wrapper">\s*<h(\d) id="(.*?)">(?=.*[\u4e00-\u9fa5])(.*?)<\/h(\d)>\s*<a class="heading-link" href="#(.*?)" aria-label="(.*?)">#<\/a>\s*<\/div>/g;
const titleRegexp = /<div class="header-wrapper">\s*<h(\d) id="(.*?)">(?!.*[\u4e00-\u9fa5])(.*?)<\/h\1>\s*<a class="heading-link" href="#(.*?)" aria-label="(.*?)">#<\/a>\s*<\/div>\s*<div class="header-wrapper">\s*<h\1 id="(.*?)">(?=.*[\u4e00-\u9fa5])(.*?)<\/h\1>\s*<a class="heading-link" href="#(.*?)" aria-label="(.*?)">#<\/a>\s*<\/div>/g;
// 正文分级标题(no_toc) 正则匹配
const titleNoTocRegexp = /<div class="header-wrapper">\s*<h(\d) class="no_toc" id="(.*?)">(?!.*[\u4e00-\u9fa5])(.*?)<\/h(\d)>\s*<a class="heading-link" href="#(.*?)" aria-label="(.*?)">#<\/a>\s*<\/div>\s*<div class="header-wrapper">\s*<h(\d) class="no_toc" id="(.*?)">(?=.*[\u4e00-\u9fa5])(.*?)<\/h(\d)>\s*<a class="heading-link" href="#(.*?)" aria-label="(.*?)">#<\/a>\s*<\/div>/g;
const titleNoTocRegexp = /<div class="header-wrapper">\s*<h(\d) class="no_toc" id="(.*?)">(?!.*[\u4e00-\u9fa5])(.*?)<\/h\1>\s*<a class="heading-link" href="#(.*?)" aria-label="(.*?)">#<\/a>\s*<\/div>\s*<div class="header-wrapper">\s*<h\1 class="no_toc" id="(.*?)">(?=.*[\u4e00-\u9fa5])(.*?)<\/h\1>\s*<a class="heading-link" href="#(.*?)" aria-label="(.*?)">#<\/a>\s*<\/div>/g;

return gulp.src(['../../_site/**/*.html'])
return gulp.src(gulpSrc)
// 正文分级标题
.pipe(
replace(titleRegexp, (match, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12) => {
replace(titleRegexp, (match, p1, p2, p3, p4, p5, p6, p7, p8, p9) => {
return `
<div class="header-wrapper">
<h${p1} id="${p2}">${p3}</h${p1}><h${p1} id="${p2}">${p9}</h${p1}>
<a class="heading-link" href="#${p5}" aria-label="${p6}">#</a>
<h${p1} id="${p2}">${p3}</h${p1}><h${p1} id="${p2}">${p7}</h${p1}>
<a class="heading-link" href="#${p4}" aria-label="${p5}">#</a>
</div>
`;
})
)
// 正文分级标题(no_toc)
.pipe(
replace(titleNoTocRegexp, (match, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12) => {
replace(titleNoTocRegexp, (match, p1, p2, p3, p4, p5, p6, p7, p8, p9) => {
return `
<div class="header-wrapper">
<h${p1} class="no_toc" id="${p2}">${p3}</h${p1}><h${p1} class="no_toc" id="${p2}">${p9}</h${p1}>
<a class="heading-link" href="#${p5}" aria-label="${p6}">#</a>
<h${p1} class="no_toc" id="${p2}">${p3}</h${p1}><h${p1} class="no_toc" id="${p2}">${p7}</h${p1}>
<a class="heading-link" href="#${p4}" aria-label="${p5}">#</a>
</div>
`;
})
Expand Down

0 comments on commit 51b850a

Please sign in to comment.