方法一、通过数据库里的html代码获取
// 通过块id获取数据库id
async function getAvIdByAvBlockId(blockId) {
const av = await getAvBySql(`SELECT * FROM blocks where type ='av' and id='${blockId}'`);
if(av.length === 0) return error("未找到数据库文档块,请检查数据库文档块id是否正确");
const avId = av.map(av => getDataAvIdFromHtml(av.markdown))[0];
return avId || '';
}
// 通过sql获取数据库信息
async function getAvBySql(sql) {
const result = await requestApi('/api/query/sql', {"stmt": sql});
if(result.code !== 0){
console.error("查询数据库出错", result.msg);
return [];
}
return result.data;
}
// 从数据库HTML代码中获取数据库id
function getDataAvIdFromHtml(htmlString) {
// 使用正则表达式匹配data-av-id的值
const match = htmlString.match(/data-av-id="([^"]+)"/);
if (match && match[1]) {
return match[1]; // 返回匹配的值
}
return ""; // 如果没有找到匹配项,则返回空
}
方法二、通过思源api先获取dom,再获取avId
// 通过块id获取数据库id
async function getAvIdByAvBlockId(blockId) {
const av = await getAvDomByBlockId(blockId);
if(!av || av.code !== 0) return error("未找到数据库文档块,请检查数据库文档块id是否正确");
const avId = av.map(av => getDataAvIdFromHtml(av?.data?.dom))[0];
return avId || '';
}
// 通过sql获取数据库信息
async function getAvDomByBlockId(blockId) {
const result = await requestApi('/api/block/getBlockDOM', {"id": blockId});
if(!result || result.code !== 0){
console.error("获取数据库DOM失败", result.msg);
return null;
}
return result;
}
// 从数据库HTML代码中获取数据库id
function getDataAvIdFromHtml(htmlString) {
// 使用正则表达式匹配data-av-id的值
const match = htmlString.match(/data-av-id="([^"]+)"/);
if (match && match[1]) {
return match[1]; // 返回匹配的值
}
return ""; // 如果没有找到匹配项,则返回空
}
