diff --git a/src/_constants.ts b/src/_constants.ts index 26bd519..54a0517 100644 --- a/src/_constants.ts +++ b/src/_constants.ts @@ -15,6 +15,7 @@ export const RFC = { export const DEFAULT_SETTINGS: HackerNewsSettings = { defaultRefreshInterval: "60", + defaultNumStories: "10", storiesFolder: "HackerNews", storyTemplate: `--- date: {{date}} diff --git a/src/apiManager.ts b/src/apiManager.ts index 19876b1..6807c29 100644 --- a/src/apiManager.ts +++ b/src/apiManager.ts @@ -14,7 +14,7 @@ export default class APIManager { this.plugin = plugin; } - public async requestTopHN(): Promise { + public async requestTopHN(): Promise> { let itemIds: Array; try { const url = "https://hacker-news.firebaseio.com/v0/topstories.json"; @@ -24,11 +24,16 @@ export default class APIManager { return Promise.reject(error); } - const itemId = itemIds[Math.floor(Math.random() * itemIds.slice(0, 25).length)] - const itemResponse = await fetch(`https://hacker-news.firebaseio.com/v0/item/${itemId}.json?print=pretty`); - const hnItem = (await itemResponse.json()) as HNItem - return hnItem; + let hnItems: Array = []; + let numStories = parseInt(this.plugin.settings.defaultNumStories); + for (let i = 0; i < numStories; i++) { + var itemId = itemIds[Math.floor(Math.random() * itemIds.slice(0, 25).length)] + const itemResponse = await fetch(`https://hacker-news.firebaseio.com/v0/item/${itemId}.json?print=pretty`); + hnItems.push((await itemResponse.json()) as HNItem); + } + + return hnItems; } public async saveHNItem(hnItem: HNItem) { diff --git a/src/l10n/locale/en.ts b/src/l10n/locale/en.ts index 817e22a..de9b7fd 100644 --- a/src/l10n/locale/en.ts +++ b/src/l10n/locale/en.ts @@ -12,6 +12,8 @@ export default { 'The folder that holds the saved HackerNews stories. The folder will be created if it does not exist.': 'The folder that holds the saved HackerNews stories. The folder will be created if it does not exist.', 'Story Template': 'Story Template', 'Specify how the HackerNews story is saved; available attributes: title, url, date.': 'Specify how the HackerNews story is saved; available attributes: title, url, date.', + 'Number of Stories': 'Number of Stories', + 'The number of top stories to fetch from HackerNews. Default and invalid values will be reverted to 10.': 'The number of top stories to fetch from HackerNews. Default and invalid values will be reverted to 10.', 'Donate': 'Donate', 'If you found this plugin helpful, consider donating to support continued development.': 'If you found this plugin helpful, consider donating to support continued development.', }; diff --git a/src/types.ts b/src/types.ts index d37f3bf..9092be1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,6 @@ export interface HackerNewsSettings { defaultRefreshInterval: string; + defaultNumStories: string; storiesFolder: string; storyTemplate: string; } diff --git a/src/ui/hackernews/hackernewsView.svelte b/src/ui/hackernews/hackernewsView.svelte index ec34c0f..1bffb3f 100644 --- a/src/ui/hackernews/hackernewsView.svelte +++ b/src/ui/hackernews/hackernewsView.svelte @@ -6,16 +6,16 @@ export let manager: APIManager; export let refreshInterval: string; - let dataHN: HNItem; + let dataHN: Array; export async function fetchTopHN() { - console.log('fetching top story from HackerNews'); + console.log('fetching top stories from HackerNews'); dataHN = await manager.requestTopHN(); } - export async function saveHNItem() { - console.log(`saving story ${dataHN.title}`); - await manager.saveHNItem(dataHN); + export async function saveHNItem(itemHN: HNItem) { + console.log(`saving story ${itemHN.title}`); + await manager.saveHNItem(itemHN); } addEventListener("obsidian-hackernews-fetchTopHN", fetchTopHN); @@ -26,20 +26,22 @@
- {#if dataHN} +

+ Refreshes every { refreshInterval } seconds. +

+ {#if dataHN }
-
- { dataHN.title } -
-

- Save - • - Read now -

-

- Refreshes every { refreshInterval } seconds. -

-
+ {#each dataHN as itemHN } +
+ { itemHN.title } +
+

+ Save + • + Read now +

+
+ {/each}
{/if}
diff --git a/src/ui/hackernews/hackernewsView.ts b/src/ui/hackernews/hackernewsView.ts index 2dc323a..ce63053 100644 --- a/src/ui/hackernews/hackernewsView.ts +++ b/src/ui/hackernews/hackernewsView.ts @@ -40,6 +40,7 @@ export default class HackerNewsView extends ItemView { }); this._view.$set({ refreshInterval: this.plugin.settings.defaultRefreshInterval, + numStories: this.plugin.settings.defaultNumStories, }); return super.onOpen(); } diff --git a/src/ui/settings/settingsTab.ts b/src/ui/settings/settingsTab.ts index d970b01..eb3a5bd 100644 --- a/src/ui/settings/settingsTab.ts +++ b/src/ui/settings/settingsTab.ts @@ -50,6 +50,18 @@ export default class SettingsTab extends PluginSettingTab { plugin.settings.storyTemplate = value; await this.save(); })); + new Setting(containerEl) + .setName(t('Number of Stories')) + .setDesc(t('The number of top stories to fetch from HackerNews. Default and invalid values will be reverted to 10.')) + .addText(text => text + .setPlaceholder('10') + .setValue(plugin.settings.defaultNumStories) + .onChange(async (value) => { + let numStories = parseInt(value) + if (Number.isNaN(numStories) || numStories <= 0) { numStories = 10 } + plugin.settings.defaultNumStories = `${numStories}`; + await this.save(); + })); new Setting(containerEl) .setName(t('Donate')) .setDesc(t('If you found this plugin helpful, consider donating to support continued development.'))