Skip to content

[Feature] List multiple HN posts #8

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 2 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
1 change: 1 addition & 0 deletions src/_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const RFC = {

export const DEFAULT_SETTINGS: HackerNewsSettings = {
defaultRefreshInterval: "60",
defaultNumStories: "10",
storiesFolder: "HackerNews",
storyTemplate: `---
date: {{date}}
Expand Down
15 changes: 10 additions & 5 deletions src/apiManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class APIManager {
this.plugin = plugin;
}

public async requestTopHN(): Promise<HNItem> {
public async requestTopHN(): Promise<Array<HNItem>> {
let itemIds: Array<Number>;
try {
const url = "https://hacker-news.firebaseio.com/v0/topstories.json";
Expand All @@ -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<HNItem> = [];
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) {
Expand Down
2 changes: 2 additions & 0 deletions src/l10n/locale/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
};
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface HackerNewsSettings {
defaultRefreshInterval: string;
defaultNumStories: string;
storiesFolder: string;
storyTemplate: string;
}
38 changes: 20 additions & 18 deletions src/ui/hackernews/hackernewsView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
export let manager: APIManager;
export let refreshInterval: string;

let dataHN: HNItem;
let dataHN: Array<HNItem>;

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);
Expand All @@ -26,20 +26,22 @@
</script>

<div class="main">
{#if dataHN}
<p class="hn-meta">
Refreshes every { refreshInterval } seconds.
</p>
{#if dataHN }
<div class="results">
<div class="container">
<a href="{ dataHN.url }" target="_blank" class="hn-link">{ dataHN.title }</a>
<br />
<p class="hn-read">
<a href="/" on:click={saveHNItem}>Save</a>
<a href="{ dataHN.url }" target="_blank">Read now</a>
</p>
<p class="hn-meta">
Refreshes every { refreshInterval } seconds.
</p>
</div>
{#each dataHN as itemHN }
<div class="container">
<a href="{ itemHN.url }" target="_blank" class="hn-link">{ itemHN.title }</a>
<br />
<p class="hn-read">
<a href="/" on:click={saveHNItem(itemHN)}>Save</a>
<a href="{ itemHN.url }" target="_blank">Read now</a>
</p>
</div>
{/each}
</div>
{/if}
</div>
Expand Down
1 change: 1 addition & 0 deletions src/ui/hackernews/hackernewsView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
12 changes: 12 additions & 0 deletions src/ui/settings/settingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.'))
Expand Down