Skip to content

add all Headings to every List-Item #2330

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
30 changes: 24 additions & 6 deletions src/data-import/markdown-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,27 @@ export function parseLists(
);

// Find the section we belong to as well.
let sectionName = findPreviousHeader(rawElement.position.start.line, metadata.headings || []);
let sectionName = undefined;
var headers = Array.from(metadata.headings || []);
let headings = new Array();
let headingIndex = findPreviousHeader(rawElement.position.start.line, headers);
if (headingIndex >= 0) {
let section = metadata.headings![headingIndex];
sectionName = section.heading;
headings.push(section);
let level = section.level;
while (--headingIndex >= 0) {
let section = metadata.headings![headingIndex];
if (level > section.level) {
level = section.level;
headings.push(section);
if (level <= 1) {
break;
}
}
}
}
headings.reverse(); //sort in ascending order, so you can group by e.g. the Top Heading
let sectionLink = sectionName === undefined ? Link.file(path) : Link.header(path, sectionName);
let closestLink = rawElement.id === undefined ? sectionLink : Link.block(path, rawElement.id);

Expand All @@ -232,6 +252,7 @@ export function parseLists(
link: closestLink,
links: links,
section: sectionLink,
headings: headings,
text: textWithNewline,
tags: common.extractTags(textNoNewline),
line: rawElement.position.start.line,
Expand Down Expand Up @@ -415,12 +436,9 @@ export function mergeFieldGroups(target: Map<string, Literal[]>, source: Map<str
}

/** Find the header that is most immediately above the given line number. */
export function findPreviousHeader(line: number, headers: HeadingCache[]): string | undefined {
if (headers.length == 0) return undefined;
if (headers[0].position.start.line > line) return undefined;

export function findPreviousHeader(line: number, headers: HeadingCache[]): number {
let index = headers.length - 1;
while (index >= 0 && headers[index].position.start.line > line) index--;

return headers[index].heading;
return index;
}
11 changes: 11 additions & 0 deletions src/data-model/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Literal, Link, Values } from "data-model/value";
import { DataObject } from "index";
import { SListItem, SMarkdownPage } from "data-model/serialized/markdown";
import { Pos } from "obsidian";
import { HeadingCache } from "obsidian";

/** All extracted markdown file metadata obtained from a file. */
export class PageMetadata {
Expand Down Expand Up @@ -168,6 +169,8 @@ export class ListItem {
link: Link;
/** A link to the section that contains this list element; could be a file if this is not in a section. */
section: Link;
/** List of all Headings 'above' this one in ascending Order, to be able to properly group List Items */
headings: HeadingCache[];
/** The text of this list item. This may be multiple lines of markdown. */
text: string;
/** The line that this list item starts on in the file. */
Expand Down Expand Up @@ -256,6 +259,14 @@ export class ListItem {
symbol: this.symbol,
link: this.link,
section: this.section,
headings: this.headings.map(h => "#".repeat(h.level) + " " + h.heading), /*{
const heading: HeadingCache = {
heading: h.heading,
level: h.level,
position: h.position
}
return heading;
}),*/
text: this.text,
tags: Array.from(this.tags),
line: this.line,
Expand Down
4 changes: 2 additions & 2 deletions test-vault/tasks/Tasks in a specific section.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

```dataview
task
where meta(section).subpath = "Section"
group by section
where meta(section).subpath
group by headings
```