Skip to content
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

Refactor AST Processing for tags, links and tasks #65

Closed
mohamedsalem401 opened this issue Nov 22, 2023 · 3 comments
Closed

Refactor AST Processing for tags, links and tasks #65

mohamedsalem401 opened this issue Nov 22, 2023 · 3 comments
Assignees

Comments

@mohamedsalem401
Copy link
Contributor

mohamedsalem401 commented Nov 22, 2023

We will extract the conversion of source to AST from the ExtractlinksFromBody to processAST function

// This is parsing the AST for the link extraction
 const processor = unified()
   .use(markdown)
   .use([
     gfm,
     [
       remarkWikiLink,
       { pathFormat: "obsidian-short", permalinks: options?.permalinks },
     ],
     ...userRemarkPlugins,
   ]);

 const ast = processor.parse(source);

then instead of

  const bodyTags = extractTagsFromBody(source);
  const links = extractWikiLinks(options?.from || "", source, {
    permalinks: options?.permalinks,
  });

we will do:

  const AST = processAST(src)
  const bodyTags = extractTagsFromASt(AST);
  const links = extractWikiLinks(options?.from || "", AST, {
    permalinks: options?.permalinks,
  });
  // TODO
  const tasks = extractTasks(AST)
@mohamedsalem401
Copy link
Contributor Author

@rufuspollock,

We can optimize our process even more by iterating through all the nodes of the AST in a single pass. Currently, we perform the following steps:

For Links:

links: source => AST => select all link nodes, select all wiki nodes, select all image nodes

For Tags:

tags: source => AST => select all nodes => get text nodes with a tag e.g., `#Tag`

To improve efficiency, we can consider the following approach:

AST => select all nodes =>
  if (node is a wikilink) => handle wiki links
  if (node is an image) => handle image
  if (node is a tag) => handle tag
  if (node is a task) => handle task

return {tags, links, tasks}

This way, we can consolidate the node selection and handle each node type in a more streamlined manner, potentially saving processing time.

@rufuspollock
Copy link
Member

@mohamedsalem401 i wouldn't think too much about optimization at this point - more about the simplicity of the code. (i know i wasn't completely clear on this). so i think the first example is correct. The overall signature for our functions extracting info will be something like:

const listOfXXX = extractXxx(ast, options)

rufuspollock added a commit that referenced this issue Nov 22, 2023
[#65, refactor ast for tags/links]
@rufuspollock
Copy link
Member

FIXED in #66

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants