random Insert other file heading in CurrentFile as a link

Use case or problem

random Insert other file heading in CurrentFile as a link

  1. Use the code quickadd or daveview

  2. heading level can be modified in the code

  3. The number of random titles can be modified in the code

Proposed solution

I want to open the current file, at any time insert another file title link, the title is random, and in the code can control the random title

Due to the use of translation software in this post, there may be grammar or unclear expressions. If there is that place that is not clear, please pay attention, thank you

Current workaround list code

### Use case or problem

random Insert other file heading in CurrentFile as a link

1. Use the code quickadd or daveview 
 
2. heading level can be modified in the code 
 
3. The number of random titles can be modified in the code

### Proposed solution

I want to open the current file, at any time insert another file title link, the title is random, and in the code can control the random title

Due to the use of translation software in this post, there may be grammar or unclear expressions. If there is that place that is not clear, please pay attention, thank you 
 
### Current workaround  list code

<%* 
const dv = this.app.plugins.plugins["dataview"].api;

const noOfNotes = 3

const result = await dv.query(`
  LIST rows[0].item.children.text
  FROM "北海道.md"
  FLATTEN file.lists as item
  WHERE !item.parent
  GROUP BY "<!-- " + item.position.start.line + "-->" + item.text
`)

for (let i = 0; i < noOfNotes; i++) {

 // Pick one random item
const oneItem = result.value.values[ Math.trunc((Math.random()* result.value.values.length))]

tR += "\n" + dv.markdownList(dv.array(oneItem))
  .replace(/<!--.*-->/, "")
  .replace(/(- .*):\s*$/gm, "$1")
}

%>

Template parsing error, aborting.
Cannot find module ‘obsidian’
Require stack:

  • electron/js2c/renderer_init
<%*
const app = require('obsidian');
const fs = require('fs-extra');
const path = require('path');
const { parseMarkdown } = require('obsidian-markdown-parser');

const customizeFilePath = '北海道.md';
const absoluteCustomizeFilePath = path.join(app.vault.adapter.basePath, customizeFilePath);

// 检查文件是否存在
if (!fs.existsSync(absoluteCustomizeFilePath)) {
  console.error('File not found:', absoluteCustomizeFilePath);
  return;
}

let fileContent = fs.readFileSync(absoluteCustomizeFilePath, 'utf8');

const markdownParser = new parseMarkdown.Parser();
const tokens = markdownParser.parse(fileContent);
const headings = [];

for (let token of tokens) {
  if (token.type === 'heading' && token.depth <= 2) { // 你可以修改 depth 来选择不同级别的标题
    headings.push(token.content);
  }
}

const numTitlesToInsert = 3;
if (headings.length < numTitlesToInsert) {
  console.error('Not enough headings to insert.');
  return;
}

const randomHeadings = headings.sort(() => 0.5 - Math.random()).slice(0, numTitlesToInsert);

// 获取当前激活的编辑器实例
const leaf = app.workspace.getActiveLeaf();
if (!leaf || !leaf.view || !leaf.view.editor) {
  console.error('No active editor found.');
  return;
}

const editor = leaf.view.editor;

for (let heading of randomHeadings) {
  const linkText = `[[${customizeFilePath}#${heading}]]`;
  editor.insertText(linkText + '\n');
}
%>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.