Skip to content

Commit

Permalink
v2.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
caol64 committed Dec 6, 2024
1 parent a091414 commit b9723f1
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 65 deletions.
8 changes: 4 additions & 4 deletions WenYan.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@
ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = YES;
CODE_SIGN_ENTITLEMENTS = WenYan/WenYan.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 12;
CURRENT_PROJECT_VERSION = 13;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"WenYan/Preview Content\"";
DEVELOPMENT_TEAM = NNQB2TH9QC;
Expand All @@ -423,7 +423,7 @@
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 2.3;
MARKETING_VERSION = 2.3.1;
PRODUCT_BUNDLE_IDENTIFIER = com.yztech.WenYan;
PRODUCT_MODULE_NAME = WenYan;
PRODUCT_NAME = "文颜";
Expand All @@ -443,7 +443,7 @@
ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = YES;
CODE_SIGN_ENTITLEMENTS = WenYan/WenYan.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 12;
CURRENT_PROJECT_VERSION = 13;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"WenYan/Preview Content\"";
DEVELOPMENT_TEAM = NNQB2TH9QC;
Expand All @@ -468,7 +468,7 @@
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 2.3;
MARKETING_VERSION = 2.3.1;
PRODUCT_BUNDLE_IDENTIFIER = com.yztech.WenYan;
PRODUCT_MODULE_NAME = WenYan;
PRODUCT_NAME = "文颜";
Expand Down
1 change: 1 addition & 0 deletions WenYan/Resources.bundle/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
};
</script>
<script src="mathjax/tex-svg-full.min.js"></script>
<script src="csstree/csstree.js"></script>
<script src="main.js" defer></script>
</head>
<body>
Expand Down
127 changes: 87 additions & 40 deletions WenYan/Resources.bundle/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,54 @@ function getContentWithMathImg() {
return clonedWenyan.outerHTML;
}
function getContentForGzh() {
const ast = csstree.parse(customCss, {
context: 'stylesheet',
positions: false,
parseAtrulePrelude: false,
parseCustomProperty: false,
parseValue: false
});

const ast1 = csstree.parse(highlightCss, {
context: 'stylesheet',
positions: false,
parseAtrulePrelude: false,
parseCustomProperty: false,
parseValue: false
});

ast.children.appendList(ast1.children);

const wenyan = document.getElementById("wenyan");
const clonedWenyan = wenyan.cloneNode(true);

csstree.walk(ast, {
visit: 'Rule',
enter(node, item, list) {
const selectorList = node.prelude.children;
selectorList.forEach((selectorNode) => {
const selector = csstree.generate(selectorNode);
// console.log(selector);

const declarations = node.block.children.toArray();
if (selector === "#wenyan") {
declarations.forEach((decl) => {
const value = csstree.generate(decl.value);
clonedWenyan.style[decl.property] = value
});
} else {
const elements = clonedWenyan.querySelectorAll(selector);
elements.forEach((element) => {
declarations.forEach((decl) => {
const value = csstree.generate(decl.value);
element.style[decl.property] = value
});
});
}
});
}
});

// 处理公式
let elements = clonedWenyan.querySelectorAll("mjx-container");
elements.forEach(element => {
Expand All @@ -159,55 +205,56 @@ function getContentForGzh() {
parent.setAttribute("style", "text-align: center; margin-bottom: 1rem;");
}
});
// 读取主题css样式
const stylesheets = document.styleSheets;
let stylesMap = new Map();
Array.from(stylesheets).forEach(sheet => {
Array.from(sheet.cssRules).forEach(rule => {
if (rule instanceof CSSStyleRule) {
if (rule.selectorText.startsWith("#wenyan")) {
let styleObject = new Map();
for (let i = 0; i < rule.style.length; i++) {
const property = rule.style[i];
styleObject.set(property, rule.style.getPropertyValue(property));
}
stylesMap.set(rule.selectorText, styleObject);
}
// 处理代码块
elements = clonedWenyan.querySelectorAll("pre code");
elements.forEach(element => {
element.innerHTML = element.innerHTML.replace(/\n/g, "<br>");
element.childNodes.forEach((node) => {
if (node.nodeType === Node.TEXT_NODE) {
const updatedContent = node.textContent.replace(/ /g, "&nbsp;");
const span = document.createElement("span");
span.innerHTML = updatedContent;
node.replaceWith(span);
}
});
});
// console.log(stylesMap);
// 公众号不支持css伪元素,将伪元素样式提取出来拼接成一个span
elements = clonedWenyan.querySelectorAll('h1, h2, h3, h4, h5, h6, blockquote');
elements.forEach(element => {
const afterRresults = Array.from(stylesMap)
.filter(([key, value]) =>
key.includes(element.tagName.toLowerCase() + "::after")
)
.map(([key, value]) => value).reduce((acc, map) => {
map.forEach((value, key) => {
acc.set(key, value);
});
return acc;
}, new Map());
const beforeRresults = Array.from(stylesMap)
.filter(([key]) =>
key.includes(element.tagName.toLowerCase() + "::before")
)
.map(([key, value]) => value).reduce((acc, map) => {
map.forEach((value, key) => {
acc.set(key, value);
});
return acc;
}, new Map());
if (afterRresults.size > 0) {
element.appendChild(buildPseudoSpan(afterRresults));
const afterResults = new Map();
const beforeResults = new Map();
csstree.walk(ast, {
visit: 'Rule',
enter(node) {
const selector = csstree.generate(node.prelude); // 生成选择器字符串
const tagName = element.tagName.toLowerCase();

// 检查是否匹配 ::after 或 ::before
if (selector.includes(`${tagName}::after`)) {
extractDeclarations(node, afterResults);
} else if (selector.includes(`${tagName}::before`)) {
extractDeclarations(node, beforeResults);
}
}
});
if (afterResults.size > 0) {
element.appendChild(buildPseudoSpan(afterResults));
}
if (beforeResults.size > 0) {
element.insertBefore(buildPseudoSpan(beforeResults), element.firstChild);
}
if (beforeRresults.size > 0) {
element.insertBefore(buildPseudoSpan(beforeRresults), element.firstChild);
});
return `${clonedWenyan.outerHTML.replace(/class="mjx-solid"/g, 'fill="none" stroke-width="70"')}`;
}
function extractDeclarations(ruleNode, resultMap) {
csstree.walk(ruleNode.block, {
visit: 'Declaration',
enter(declNode) {
const property = declNode.property;
const value = csstree.generate(declNode.value);
resultMap.set(property, value);
}
});
return `${clonedWenyan.outerHTML.replace(/class="mjx-solid"/g, 'fill="none" stroke-width="70"')}<style>${removeComments(customCss)}${removeComments(highlightCss)}</style>`;
}
function getContentForMedium() {
const wenyan = document.getElementById("wenyan");
Expand Down
4 changes: 1 addition & 3 deletions WenYan/Resources.bundle/themes/gzh_default.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
#wenyan {
font-family: var(--sans-serif-font);
line-height: 1.75;
font-size: 16px;
}
/* 全局子元素属性 */
#wenyan * {
box-sizing: border-box;
}
/* 支持分组 */
#wenyan h1,
#wenyan h2,
Expand Down
4 changes: 1 addition & 3 deletions WenYan/Resources.bundle/themes/lapis.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
color: var(--text-color);
font-family: var(--sans-serif-font);
line-height: 1.75;
}
#wenyan * {
box-sizing: border-box;
font-size: 16px;
}
#wenyan p,
#wenyan pre {
Expand Down
4 changes: 1 addition & 3 deletions WenYan/Resources.bundle/themes/maize.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
#wenyan {
font-family: var(--sans-serif-font);
line-height: 1.75;
}
#wenyan * {
box-sizing: border-box;
font-size: 16px;
}
#wenyan p,
#wenyan pre {
Expand Down
4 changes: 1 addition & 3 deletions WenYan/Resources.bundle/themes/orangeheart.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
#wenyan {
font-family: var(--sans-serif-font);
line-height: 1.75;
}
#wenyan * {
box-sizing: border-box;
font-size: 16px;
}
#wenyan p,
#wenyan pre {
Expand Down
4 changes: 1 addition & 3 deletions WenYan/Resources.bundle/themes/pie.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
line-height: 1.75;
color: var(--mid-10);
letter-spacing: 0;
}
#wenyan * {
box-sizing: border-box;
font-size: 16px;
}
#wenyan p,
#wenyan pre {
Expand Down
4 changes: 1 addition & 3 deletions WenYan/Resources.bundle/themes/purple.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
font-family: var(--base-font);
color: var(--text-color);
line-height: 1.75;
}
#wenyan * {
box-sizing: border-box;
font-size: 16px;
}
#wenyan a {
border-bottom: 1px solid var(--link-bottom);
Expand Down
4 changes: 1 addition & 3 deletions WenYan/Resources.bundle/themes/rainbow.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
#wenyan {
font-family: var(--sans-serif-font);
line-height: 1.75;
}
#wenyan * {
box-sizing: border-box;
font-size: 16px;
}
#wenyan p,
#wenyan pre {
Expand Down

0 comments on commit b9723f1

Please sign in to comment.