Skip to content

Commit 68572ae

Browse files
authored
Merge pull request #12 from akopdev/add-quick-add
Fetch BibTeX entries from arXiv
2 parents 5cde29c + 2d06f85 commit 68572ae

File tree

13 files changed

+214
-10
lines changed

13 files changed

+214
-10
lines changed

.github/workflows/beta-build.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Build beta versions
2+
3+
on:
4+
push:
5+
branches:
6+
- '*' # Matches all branches
7+
- '!master' # Excludes master branch
8+
9+
env:
10+
PLUGIN_NAME: bibtex-manager
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v2
17+
18+
- name: Use Node.js
19+
uses: actions/setup-node@v1
20+
with:
21+
node-version: "21.x"
22+
23+
- name: Build
24+
run: |
25+
yarn
26+
yarn run build --if-present
27+
mkdir ${{ env.PLUGIN_NAME }}
28+
cp main.js manifest.json styles.css ${{ env.PLUGIN_NAME }}
29+
zip -r ${{ env.PLUGIN_NAME }}.zip ${{ env.PLUGIN_NAME }}
30+
ls
31+
32+
- name: Upload build artifacts
33+
uses: actions/upload-artifact@v3
34+
with:
35+
name: ${{ env.PLUGIN_NAME }}-artifacts
36+
path: |
37+
${{ env.PLUGIN_NAME }}/
38+
${{ env.PLUGIN_NAME }}.zip
39+
main.js
40+
manifest.json
41+
styles.css
42+

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ start: init
2828
build:
2929
@npm run build
3030

31+
# -------------------------------------------------------------------------------------------------
32+
# version: @ Update the version of the extension
33+
# -------------------------------------------------------------------------------------------------
34+
version:
35+
@npm run version
36+
3137
# -------------------------------------------------------------------------------------------------
3238
# release: @ Create a new release
3339
# -------------------------------------------------------------------------------------------------

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ You can add BibTeX entries from Zotero, Mendeley, or any other popular reference
2222
- Generate literature notes from BibTeX entries using customizable templates.
2323
- Automatically format your BibTeX entries as a reference list, creating a well-organized bibliography within your notes.
2424
- Insert citations directly into your notes.
25+
- Fetch BibTeX entries from remote sources.
2526

2627
## Installation
2728

manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"id": "bibtex-manager",
33
"name": "BibTeX Manager",
4-
"version": "0.8.12",
4+
"version": "0.9.0",
55
"minAppVersion": "1.5.0",
66
"description": "Create a literature notes from a BibTeX entries.",
77
"author": "Akop Kesheshyan",
88
"authorUrl": "https://github.com/akopdev",
99
"isDesktopOnly": false
10-
}
10+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"name": "bibtex-manager",
3+
"version": "0.9.0",
34
"description": "Create notes and citations from BibTeX files",
45
"main": "main.js",
56
"scripts": {
67
"dev": "node esbuild.config.mjs",
7-
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production"
8+
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
9+
"version": "node version-bump.mjs && git add manifest.json versions.json"
810
},
911
"keywords": [],
1012
"author": "Akop Kesheshyan",

src/components/modals/base.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
import { App, Modal, Notice, TFile, TFolder, Setting } from "obsidian";
1+
import { App, Modal, Notice, TFile, TFolder, Setting, TextAreaComponent } from "obsidian";
22
import { BibTeXTypes, Settings } from "../settings";
33
import { Citation } from "../../modules";
44
import { Entry } from "@retorquere/bibtex-parser/grammar";
5+
import { BibTexProviders, BibTexProvider } from "../providers";
56

67
export class BaseModal extends Modal {
78
bibtex: string;
89
template: string;
910
settings: Settings;
1011
title: string;
12+
text: TextAreaComponent;
13+
fetchFrom: string;
14+
provider: BibTexProvider | null;
1115

1216
constructor(app: App, settings: Settings) {
1317
super(app);
@@ -66,7 +70,7 @@ export class BaseModal extends Modal {
6670
const abstractFile = this.app.vault.getAbstractFileByPath(folder);
6771
if (abstractFile instanceof TFolder) {
6872
return abstractFile;
69-
}
73+
}
7074
}
7175

7276
let lastFile = this.app.workspace.getActiveFile();
@@ -100,8 +104,52 @@ export class BaseModal extends Modal {
100104

101105
contentEl.createEl("h2", { text: `${this.title} a note` });
102106

107+
new Setting(contentEl)
108+
.addSearch((text) => {
109+
text.setPlaceholder("Enter a URL or arXiv ID").onChange((value) => {
110+
this.provider = null;
111+
if (!value) {
112+
text.inputEl.removeClass("bibtex-manager-quick-add--not-found");
113+
return;
114+
}
115+
for (const provider of BibTexProviders) {
116+
const p = new provider(value)
117+
const match = p.match();
118+
if (match) {
119+
this.provider = p;
120+
break;
121+
}
122+
}
123+
text.inputEl.toggleClass("bibtex-manager-quick-add--not-found", !!!this.provider);
124+
});
125+
})
126+
.addButton((btn) =>
127+
btn
128+
.setButtonText("Fetch")
129+
.setCta()
130+
.onClick(async () => {
131+
if (this.provider) {
132+
btn.setDisabled(true);
133+
btn.setButtonText("Fetching...");
134+
const bibtex = await this.provider.fetch();
135+
btn.setDisabled(false);
136+
btn.setButtonText("Fetch");
137+
if (bibtex) {
138+
this.bibtex = bibtex;
139+
this.text.setValue(bibtex);
140+
}
141+
}
142+
}),
143+
)
144+
.setClass("bibtex-manager-quick-add");
145+
146+
// Separator with "or"
147+
const separator = contentEl.createEl("div", { cls: "bibtex-manager-separator" });
148+
separator.createSpan({ cls: "bibtex-manager-separator-text", text: "or" });
149+
103150
new Setting(contentEl)
104151
.addTextArea((text) => {
152+
this.text = text;
105153
text.setPlaceholder(
106154
"Paste the content of your BibTeX file",
107155
).onChange((value) => {

src/components/providers/arxiv.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { requestUrl, RequestUrlResponse } from "obsidian";
2+
import { BibTexProvider } from "./base";
3+
4+
export class ArXivProvider implements BibTexProvider {
5+
6+
id: string | null = null;
7+
8+
constructor(private url: string) {
9+
this.url = url;
10+
}
11+
12+
match(): boolean {
13+
const id = this.url.match(/\s*(?:arXiv:\s*|arxiv\.org\/\s*(?:abs|pdf)\s*\/)\s*(\d{4}\.\d{5})\s*/i)
14+
this.id = id ? id[1] : null;
15+
return !!this.id;
16+
}
17+
18+
async fetch() {
19+
if (!this.match()) {
20+
return;
21+
}
22+
try {
23+
const response: RequestUrlResponse = await requestUrl({
24+
url: `https://arxiv.org/bibtex/${this.id}`
25+
});
26+
if (response.status === 200) {
27+
return response.text
28+
} else {
29+
throw new Error(`Failed to fetch data. Status: ${response.status}`);
30+
}
31+
} catch (error) {
32+
throw error;
33+
}
34+
35+
}
36+
}

src/components/providers/base.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface BibTexProvider {
2+
match(): boolean;
3+
fetch(): Promise<string>;
4+
}

src/components/providers/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./base";
2+
export * from "./providers";
3+

src/components/providers/providers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { BibTexProvider } from './base';
2+
import { ArXivProvider } from './arxiv';
3+
4+
type BibTexProviderConstructor = new (url: string) => BibTexProvider;
5+
6+
export const BibTexProviders: BibTexProviderConstructor[] = [
7+
ArXivProvider,
8+
];

styles.css

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,50 @@
1919
min-height: 200px;
2020
}
2121

22-
.bibtex-manager-template-desc{
22+
.bibtex-manager-template-desc {
2323
display: block;
2424
color: var(--text-muted);
25-
font-size: var(--font-ui-smaller);
26-
margin-bottom: var(--size-4-4);
27-
line-height: var(--line-height-tight);
25+
font-size: var(--font-ui-smaller);
26+
margin-bottom: var(--size-4-4);
27+
line-height: var(--line-height-tight);
2828
}
29-
.bibtex-manager-template-desc a{
29+
30+
.bibtex-manager-template-desc a {
3031
margin-block-start: 0;
3132
}
33+
34+
.bibtex-manager-quick-add .setting-item-info {
35+
display: none;
36+
}
37+
38+
.bibtex-manager-quick-add .search-input-container {
39+
width: 100%;
40+
}
41+
42+
.bibtex-manager-quick-add--not-found {
43+
border-color: var(--background-modifier-error) !important;
44+
}
45+
46+
.bibtex-manager-quick-add--not-found:focus {
47+
box-shadow: 0 0 0 2px var(--background-modifier-error) !important;
48+
}
49+
50+
.is-mobile .bibtex-manager-quick-add .setting-item-control button {
51+
max-width: 80px !important;
52+
}
53+
54+
.bibtex-manager-separator {
55+
display: flex;
56+
align-items: center;
57+
margin: 20px 0;
58+
text-align: center;
59+
}
60+
61+
62+
.bibtex-manager-separator::before,
63+
.bibtex-manager-separator::after {
64+
content: "";
65+
flex: 1;
66+
border-top: 1px solid var(--background-modifier-border);
67+
margin: 0 10px;
68+
}

version-bump.mjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { readFileSync, writeFileSync } from "fs";
2+
3+
const targetVersion = process.env.npm_package_version;
4+
5+
// read minAppVersion from manifest.json and bump version to target version
6+
let manifest = JSON.parse(readFileSync("manifest.json", "utf8"));
7+
const { minAppVersion } = manifest;
8+
manifest.version = targetVersion;
9+
writeFileSync("manifest.json", JSON.stringify(manifest, null, "\t"));
10+
11+
// update versions.json with target version and minAppVersion from manifest.json
12+
let versions = JSON.parse(readFileSync("versions.json", "utf8"));
13+
versions[targetVersion] = minAppVersion;
14+
writeFileSync("versions.json", JSON.stringify(versions, null, "\t"));

versions.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"0.9.0": "1.5.0"
3+
}

0 commit comments

Comments
 (0)