Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 支持markdown与latex同时渲染 #624

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/uni-app/components/mp-html/mp-html.vue
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,48 @@ export default {
// #endif
},

/**
* 深度清理对象中value为null的item
* @param obj 要清理的对象
* @param cache 默认的缓存对象
* @returns {{}|*} 清理后的对象
*/
deepClean(obj, cache = new WeakMap()) {
if (obj === null || typeof obj !== 'object') return obj;
if (cache.has(obj)) return cache.get(obj);
let clone;

if (obj instanceof Date) {
clone = new Date(obj.getTime());
} else if (obj instanceof RegExp) {
clone = new RegExp(obj);
} else if (obj instanceof Map) {
clone = new Map(Array.from(obj, ([key, value]) => {
return value ? [key, this.deepClean(value, cache)] : null;
}).filter(item => item !== null));
} else if (obj instanceof Set) {
clone = new Set(Array.from(obj, value => {
return value ? this.deepClean(value, cache) : null;
}).filter(item => item !== null));
} else if (Array.isArray(obj)) {
clone = obj.map(value => value ? this.deepClean(value, cache) : null)
.filter(item => item !== null);
} else if (Object.prototype.toString.call(obj) === '[object Object]') {
clone = Object.create(Object.getPrototypeOf(obj));
cache.set(obj, clone);
for (const [key, value] of Object.entries(obj)) {
if (value) {
clone[key] = this.deepClean(value, cache);
}
}
} else {
clone = Object.assign({}, obj);
}

cache.set(obj, clone);
return clone;
},

/**
* @description 设置内容
* @param {String} content html 内容
Expand All @@ -320,7 +362,7 @@ export default {
this._set(nodes, append)
}
// #endif
this.$set(this, 'nodes', append ? (this.nodes || []).concat(nodes) : nodes)
this.$set(this, 'nodes', append ? (this.nodes || []).concat(this.deepClean(nodes)) : this.deepClean(nodes))

// #ifndef APP-PLUS-NVUE
this._videos = []
Expand Down