flat: 修改
This commit is contained in:
@@ -4,6 +4,7 @@ import parseHtml from '@/lib/html-parser.js';
|
||||
// import DOMPurify from '@/lib/dompurify@3.2.4es.js';
|
||||
|
||||
export let codeDataList = []
|
||||
export let jobMoreMap = new Map()
|
||||
|
||||
const md = new MarkdownIt({
|
||||
html: true, // 允许 HTML 标签
|
||||
@@ -16,23 +17,31 @@ const md = new MarkdownIt({
|
||||
highlight: function(str, lang) {
|
||||
if (lang === 'job-json') {
|
||||
const result = safeExtractJson(str);
|
||||
const jobId = result.appJobUrl.split('jobId=')[1]
|
||||
return `
|
||||
<a class="custom-card" data-job-id="${jobId}">
|
||||
<div class="card-title">
|
||||
<span class="title-text" >${result.jobTitle}</span>
|
||||
<div class="card-salary">${result.salary}</div>
|
||||
</div>
|
||||
<div class="card-company">${result.location}·${result.companyName}</div>
|
||||
<div class="card-info">
|
||||
<div class="info-item">
|
||||
<div class="card-tag">${result.education}</div>
|
||||
<div class="card-tag">${result.experience}</div>
|
||||
if (result) { // json解析成功
|
||||
const jobId = result.appJobUrl.split('jobId=')[1]
|
||||
let domContext = `
|
||||
<a class="custom-card" data-job-id="${jobId}">
|
||||
<div class="card-title">
|
||||
<span class="title-text" >${result.jobTitle}</span>
|
||||
<div class="card-salary">${result.salary}</div>
|
||||
</div>
|
||||
<div class="info-item">查看详情<div class="position-nav"></div></div>
|
||||
</div>
|
||||
</a>
|
||||
`
|
||||
<div class="card-company">${result.location}·${result.companyName}</div>
|
||||
<div class="card-info">
|
||||
<div class="info-item">
|
||||
<div class="card-tag">${result.education}</div>
|
||||
<div class="card-tag">${result.experience}</div>
|
||||
</div>
|
||||
<div class="info-item">查看详情<div class="position-nav"></div></div>
|
||||
</div>
|
||||
</a>
|
||||
`
|
||||
if (result.data) {
|
||||
jobMoreMap.set(jobId, result.data)
|
||||
domContext +=
|
||||
`<a class="custom-more" data-job-id="${jobId}">查看更多岗位<div class="more-icon"></div></a>`
|
||||
}
|
||||
return domContext
|
||||
}
|
||||
}
|
||||
// <div class="card-tag">${result.location}</div>
|
||||
// <div class="info-item">${result.salary}</div>
|
||||
@@ -68,18 +77,54 @@ const md = new MarkdownIt({
|
||||
}
|
||||
})
|
||||
|
||||
function extractFirstJson(text) {
|
||||
let stack = [];
|
||||
let startIndex = -1;
|
||||
let endIndex = -1;
|
||||
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
const char = text[i];
|
||||
|
||||
if (char === '{') {
|
||||
if (stack.length === 0) startIndex = i; // 记录第一个 '{' 的位置
|
||||
stack.push(char);
|
||||
} else if (char === '}') {
|
||||
stack.pop();
|
||||
if (stack.length === 0) {
|
||||
endIndex = i; // 找到配对的 '}'
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (startIndex !== -1 && endIndex !== -1) {
|
||||
const jsonString = text.slice(startIndex, endIndex + 1);
|
||||
try {
|
||||
const jsonObject = JSON.parse(jsonString);
|
||||
return jsonObject;
|
||||
} catch (e) {
|
||||
return null; // 如果不是有效的 JSON
|
||||
}
|
||||
}
|
||||
|
||||
return null; // 如果没有找到有效的 JSON 对象
|
||||
}
|
||||
|
||||
|
||||
function safeExtractJson(text) {
|
||||
try {
|
||||
const match = text.match(/\{[\s\S]*?\}/); // 提取第一个完整的 JSON 块
|
||||
if (match) {
|
||||
return JSON.parse(match[0]);
|
||||
}
|
||||
const jsonObject = extractFirstJson(text);
|
||||
return jsonObject
|
||||
} catch (e) {
|
||||
console.error('JSON 解析失败:', e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function clearJobMoreMap() { // 切换对话清空
|
||||
jobMoreMap.clear()
|
||||
}
|
||||
|
||||
export function parseMarkdown(content) {
|
||||
if (!content) {
|
||||
return //处理特殊情况,比如网络异常导致的响应的 content 的值为空
|
||||
|
Reference in New Issue
Block a user