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

Computed metadata fields #54

Closed
4 tasks done
rufuspollock opened this issue Nov 16, 2023 · 5 comments
Closed
4 tasks done

Computed metadata fields #54

rufuspollock opened this issue Nov 16, 2023 · 5 comments
Assignees

Comments

@rufuspollock
Copy link
Member

rufuspollock commented Nov 16, 2023

Computed fields (or just any operations on incoming records)

cf https://www.contentlayer.dev/docs/reference/source-files/define-document-type#computedfields

When loading a file i want to create new fields based on some computation so that i can have additional metadata

  • Add a type based on the folder of the file so that i can label blog posts
  • Add layout based on the folder so i can change layouts based on folder
  • compute title frontmatter from first heading

More Examples:

  • Generate keywords based on the content of the file.
  • Label blog posts based on the folder structure.
  • Count the number of words in the content.
  • Estimate the reading time based on the content.
  • Detect the language of the content.

Acceptance

  • Design sketch with API/UX for users of lib in README or similar
  • Simple example in examples folder just a single js file like index.js
import indexFolder from markdowndb

files = indexFolder(../../__mock__, computedFields=...)

console.log(files[0].readingTime)

Tasks

  • sketch out the design before implementing
  • ...

Design

We can achieve this by allowing users to define custom fields using JavaScript functions. Each function takes the following information as parameters:

  • File path
  • File metadata (e.g., tags, title, ...)
  • File body content
  • File type (e.g., blog, ...)

The custom field generated by the function can then be added to the document scheme.

Example Implementation

// User-defined function to generate keywords based on content
function generateKeywords(filePath, metadata, content, fileType) {
  const keywords = ....
  return { keywords };
}

// Document scheme with custom field added
const documentScheme = {
  keywords: { value: generateKeywords}, // this will be excuted
};

In this example, the generateKeywords function is a placeholder for the user-defined function. Users can implement similar functions to automate the generation of custom fields based on their specific requirements. The custom field is then added to the document scheme for validation and type decleration.

Notes

@olayway
Copy link
Member

olayway commented Nov 17, 2023

@rufuspollock I've advised @mohamedsalem401 to sketch out architecture design before implementing

@mohamedsalem401
Copy link
Contributor

What do we want by implementing custom fields?

We aim to provide users with a simple way to automate field generation based on a file's data.

Examples:

  • Generate keywords based on the content of the file.
  • Label blog posts based on the folder structure.
  • Count the number of words in the content.
  • Estimate the reading time based on the content.
  • Detect the language of the content.

How can we do this?

We can achieve this by allowing users to define custom fields using JavaScript functions. Each function takes the following information as parameters:

  • File path
  • File metadata (e.g., tags, title, ...)
  • File body content
  • File type (e.g., blog, ...)

The custom field generated by the function can then be added to the document scheme.

Example Implementation:

// User-defined function to generate keywords based on content
function generateKeywords(filePath, metadata, content, fileType) {
  const keywords = ....
  return { keywords };
}

// Document scheme with custom field added
const documentScheme = {
  keywords: { value: generateKeywords}, // this will be excuted
};

In this example, the generateKeywords function is a placeholder for the user-defined function. Users can implement similar functions to automate the generation of custom fields based on their specific requirements. The custom field is then added to the document scheme for validation and type decleration.

@rufuspollock
Copy link
Member Author

@rufuspollock i've posted a design approach in the description. let me know what you think.

@rufuspollock
Copy link
Member Author

@mohamedsalem401 i think we probably want to operate on the ast ... (things are already parsed ...). at least in first pass i would try that ...

@rufuspollock
Copy link
Member Author

rufuspollock commented Nov 28, 2023

@mohamedsalem401 can you draft your spec as a PR to README.md (for a new section called "Computed Fields"

I think the function is something like:

function computeSomething(fileObject, ast) { // either returns null and changes in place OR returns a duplicate of the object with the altered fields ...
// my guess is to KISS and just alter in place for now ...
}

mohamedsalem401 added a commit that referenced this issue Dec 11, 2023
* Computed Fields

* Make configuration optional

* Add changeset for computed fields

* Update changeset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants