Skip to content

Commit

Permalink
fix: add wordsCount and readLength to the exposed variables in the ar…
Browse files Browse the repository at this point in the history
…ticle template
  • Loading branch information
sywhb committed May 11, 2023
1 parent c7e5daf commit 56813b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface Article {
publishedAt?: string;
url: string;
readAt?: string;
wordsCount?: number;
}

export interface Label {
Expand Down Expand Up @@ -101,6 +102,7 @@ export const loadArticles = async (
content
publishedAt
readAt
wordsCount
highlights {
id
quote
Expand Down
46 changes: 26 additions & 20 deletions src/settings/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,35 +47,37 @@ date_published: {{{datePublished}}}
{{/highlights}}
{{/highlights.length}}`;

export interface LabelVariable {
export interface LabelView {
name: string;
}

export interface HighlightVariables {
export interface HighlightView {
text: string;
highlightUrl: string;
dateHighlighted: string;
note?: string;
labels?: LabelVariable[];
labels?: LabelView[];
}

export interface ArticleVariables {
export interface ArticleView {
id: string;
title: string;
omnivoreUrl: string;
siteName: string;
originalUrl: string;
author?: string;
labels?: LabelVariable[];
labels?: LabelView[];
dateSaved: string;
highlights: HighlightVariables[];
highlights: HighlightView[];
content: string;
datePublished?: string;
fileAttachment?: string;
description?: string;
note?: string;
type: PageType;
dateRead?: string;
wordsCount?: number;
readLength?: number;
}

export const renderFilename = (
Expand All @@ -102,7 +104,7 @@ export const renderAttachmentFolder = (
});
};

export const renderLabels = (labels?: LabelVariable[]) => {
export const renderLabels = (labels?: LabelView[]) => {
return labels?.map((l) => ({
// replace spaces with underscores because Obsidian doesn't allow spaces in tags
name: l.name.replaceAll(" ", "_"),
Expand Down Expand Up @@ -143,17 +145,15 @@ export const renderArticleContnet = async (
}
});
}
const highlights: HighlightVariables[] = articleHighlights.map(
(highlight) => {
return {
text: formatHighlightQuote(highlight.quote, template),
highlightUrl: `https://omnivore.app/me/${article.slug}#${highlight.id}`,
dateHighlighted: formatDate(highlight.updatedAt, dateHighlightedFormat),
note: highlight.annotation,
labels: renderLabels(highlight.labels),
};
}
);
const highlights: HighlightView[] = articleHighlights.map((highlight) => {
return {
text: formatHighlightQuote(highlight.quote, template),
highlightUrl: `https://omnivore.app/me/${article.slug}#${highlight.id}`,
dateHighlighted: formatDate(highlight.updatedAt, dateHighlightedFormat),
note: highlight.annotation,
labels: renderLabels(highlight.labels),
};
});
const dateSaved = formatDate(article.savedAt, dateSavedFormat);
const siteName =
article.siteName || siteNameFromUrl(article.originalArticleUrl);
Expand All @@ -167,7 +167,11 @@ export const renderArticleContnet = async (
const dateRead = article.readAt
? formatDate(article.readAt, dateSavedFormat)
: undefined;
const articleVariables: ArticleVariables = {
const wordsCount = article.wordsCount;
const readLength = wordsCount
? Math.round(Math.max(1, wordsCount / 235))
: undefined;
const articleView: ArticleView = {
id: article.id,
title: article.title,
omnivoreUrl: `https://omnivore.app/me/${article.slug}`,
Expand All @@ -188,9 +192,11 @@ export const renderArticleContnet = async (
note: articleNote?.annotation,
type: article.pageType,
dateRead,
wordsCount,
readLength,
};
// Build content string based on template
let content = Mustache.render(template, articleVariables);
let content = Mustache.render(template, articleView);

const frontmatterRegex = /^(---[\s\S]*?---)/gm;
// get the frontmatter from the content
Expand Down

0 comments on commit 56813b7

Please sign in to comment.