fix: 一级标题没有隐藏原始标题

fix: 中文标题的 id 应该只按照英文版生成,英文内容不再设置 id
This commit is contained in:
Zhicheng Wang 2018-03-08 12:47:55 +08:00
parent 8af121ee8f
commit d24f7c74f2
5 changed files with 58 additions and 19 deletions

View File

@ -1,7 +1,7 @@
import { Inject, Injectable } from '@angular/core';
import { DOCUMENT, DomSanitizer, SafeHtml } from '@angular/platform-browser';
import { ReplaySubject } from 'rxjs/ReplaySubject';
import { ScrollSpyInfo, ScrollSpyService } from 'app/shared/scroll-spy.service';
import { ReplaySubject } from 'rxjs/ReplaySubject';
export interface TocItem {
@ -18,10 +18,10 @@ export class TocService {
activeItemIndex = new ReplaySubject<number | null>(1);
private scrollSpyInfo: ScrollSpyInfo | null;
constructor(
@Inject(DOCUMENT) private document: any,
constructor(@Inject(DOCUMENT) private document: any,
private domSanitizer: DomSanitizer,
private scrollSpyService: ScrollSpyService) { }
private scrollSpyService: ScrollSpyService) {
}
genToc(docElement?: Element, docId = '') {
this.resetScrollSpyInfo();
@ -75,7 +75,10 @@ export class TocService {
private isOriginalText(heading: HTMLHeadingElement): boolean {
if (heading && heading.hasAttribute('translation-origin')) {
const prevNode = heading.previousElementSibling;
let prevNode = heading.previousElementSibling;
if (prevNode && prevNode.tagName === 'AIO-TOC') {
prevNode = prevNode.previousElementSibling;
}
if (prevNode && prevNode.hasAttribute('translation-result')) {
return true;
}

View File

@ -2,6 +2,10 @@
display: none;
}
[translation-result] + aio-toc + [translation-origin=off] {
display: none;
}
[translation-origin=on] {
border-top: 1px dashed #0273D4;
}

View File

@ -1,14 +1,38 @@
const slugs = require('github-slugger')();
function mark(text) {
return text.replace(/<(h\d|header|p|section)(\b[\s\S]*?)>([\s\S]+?)<\/\1>/gi, function ($0, $1, $2, $3) {
const suffix = isTranslation($3) ? 'result' : 'origin=off';
return `<${$1} translation-${suffix} ${$2}>${$3}</${$1}>`;
});
text = addTranslationAttributes(text);
text = addIdsToHn(text);
return text;
}
function isTranslation(text) {
return text && /[\u2E80-\u2EFF\u2F00-\u2FDF\u3000-\u303F\u31C0-\u31EF\u3200-\u32FF\u3300-\u33FF\u3400-\u4DBF\u4DC0-\u4DFF\u4E00-\u9FBF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF]/.test(text);
}
function addTranslationAttributes(text) {
return text.replace(/<(h\d|header|p|section)(\b[\s\S]*?)>([\s\S]+?)<\/\1>/gi, function ($0, $1, $2, $3) {
const suffix = isTranslation($3) ? 'result' : 'origin="off"';
return `<${$1} translation-${suffix} ${$2}>${$3}</${$1}>`;
});
}
function addIdsToHn(text) {
return text.replace(/<(h\d) translation-origin="off" (?!\bid\b=)(.*?)>(.*?)<\/\1>\n<\1 translation-result (.*?)>(.*?)<\/\1>/gi,
(_0, _1, _2, _3, _4, _5) => {
const id = toId(_3);
return `<${_1} translation-origin="off" ${_2}>${_3}</${_1}>\n<${_1} translation-result ${_4} id="${id}">${_5}</${_1}>`;
});
}
function toId(text) {
return slugs.slug(text);
}
module.exports = {
mark,
addTranslationAttributes,
addIdsToHn,
};

View File

@ -0,0 +1,18 @@
const { expect } = require('chai');
const { mark, addTranslationAttributes, addIdsToHn } = require('./translator');
describe('translator', () => {
it('mark translation results and origins', () => {
expect(addTranslationAttributes(`<h2 class="no-toc" id="one-or-two">One or two</h2>
<h2 class="no-toc" id="一或二">一或二</h2>`))
.eql(`<h2 translation-origin="off" class="no-toc" id="one-or-two">One or two</h2>
<h2 translation-result class="no-toc" id="一或二">一或二</h2>`);
});
it('should add ids for translation', function () {
expect(addIdsToHn(`<h2 translation-origin="off" class="no-toc">One or two</h2>
<h2 translation-result class="no-toc">一或二</h2>`)).eql(`<h2 translation-origin="off" class="no-toc">One or two</h2>
<h2 translation-result class="no-toc" id="one-or-two">一或二</h2>`);
});
});

View File

@ -1,10 +0,0 @@
const { expect } = require('chai');
const { mark } = require('./translator');
describe('translator', () => {
it('mark translation results and origins', () => {
expect(mark('<h1>AngularJS to Angular</h1>\n<h1>从 AngularJS 到 Angular 快速参考</h1>'))
.toEqual('<h1 translation-origin=off >AngularJS to Angular</h1>\n<h1 translation-result >从 AngularJS 到 Angular 快速参考</h1>');
});
});