本博客由 Pipe 强力驱动

html 块自动生成子文档目录

需要在js中手动更改块id为当前文档的任意id。

<html>
<div style="font-size:20px;color:red">F5刷新文档目录</div>
<script>
    var block_id = '改成块id';
    exec();

    async function request(url, data) {
        return fetch(url, {
            body: JSON.stringify(data),
            method: 'POST'

        }).then(r => {
            if (r.status === 200)
                return r.json();
            else return null;
        });
    }

    function write_line(pArr, record, isSon) {
        var line = '';
        if (isSon) {
            line += '\n'
        }
        line += ' '.repeat(pArr.length * 2) + "* ((" + record.id + " '" + record.content + "'))\n";
        //alert(line)
        return line;
    }

    async function exec() {
        //查询所有子目录
        var sql = `select root_id from blocks where id = '${block_id}'`;
        var response_data = await querySql(sql);
        var current_root_id = response_data.data[0].root_id;
        sql = `
        SELECT  b1.id,b1.content,b1.hpath,b1.path FROM 'blocks' as b1 WHERE b1.path like '%${current_root_id}%' and b1.type = 'd' and root_id <> '${current_root_id}' order by hpath 
        `
        response_data = await querySql(sql);
        // var response_text = quickerGetVar('response_text');
        // var response_data = JSON.parse(response_text);
        var records = response_data.data;
        var markdown = '';
        var parents = [];

        for (var i = 0; i < records.length; i++) {
            var isSon = true;
            record = records[i];
            while (parents.length > 0) {
                if (record.path.indexOf(parents[parents.length - 1]) > 0) {
                    markdown += write_line(parents, record, isSon);
                    parents.push(record.id);
                    break;
                } else {
                    isSon = false;
                    parents.pop();
                }
            }
            if (parents.length == 0) {
                markdown += write_line(parents, record, false);
                parents.push(record.id);
            }
        }
        console.log(markdown.trim());

        await updateBlock(block_id, markdown.trim());
        // markdown.trim()
    }

    async function querySql(sql) {
        return request('/api/query/sql', {
            stmt: `${sql}`,
        });
    }

    async function updateBlock(id, data, dataType = 'markdown') {
        return request('/api/block/updateBlock', {
            id: id,
            data: data,
            dataType: dataType,
        });
    }



</script>
</html>
评论
留下你的脚步
0