Skip to content

Commit 85e9ad9

Browse files
Merge pull request #23 from mProjectsCode/master
version 0.2.0
2 parents 0c74d12 + 263f208 commit 85e9ad9

File tree

13 files changed

+349
-15
lines changed

13 files changed

+349
-15
lines changed

.github/ISSUE_TEMPLATE/api-request.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
name: API request
3+
about: Suggest a new API to be added to this plugin.
4+
title: ''
5+
labels: API request
6+
assignees: ''
7+
8+
---
9+
10+
**Name**
11+
Name of the API you would like to be added to the plugin
12+
13+
**Link**
14+
A link to their API documentation
15+
16+
**What does the API do/offer**
17+
A short description of what data the API offers
18+
19+
20+
- [ ] Is the API free to use
21+
- [ ] Does the API require authentication

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: bug
6+
assignees: ''
7+
8+
---
9+
10+
- [ ] The Plugin is up to date
11+
- [ ] Obsidian is up to date
12+
13+
**Describe the bug**
14+
A clear and concise description of what the bug is.
15+
16+
**To Reproduce**
17+
Steps to reproduce the behavior:
18+
1. Go to '...'
19+
2. Click on '....'
20+
3. Scroll down to '....'
21+
4. See error
22+
23+
**Expected behavior**
24+
A clear and concise description of what you expected to happen.
25+
26+
**Screenshots**
27+
If applicable, add screenshots to help explain your problem.
28+
29+
**Occurs on**
30+
- [ ] Windows
31+
- [ ] macOS
32+
- [ ] Linux
33+
- [ ] Android
34+
- [ ] iOS
35+
36+
**Plugin version**
37+
x.x.x
38+
39+
**Additional context**
40+
Add any other context about the problem here.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: feature request
6+
assignees: ''
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Additional context**
17+
Add any other context or screenshots about the feature request here.

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ Available variables that can be used in template tags are the same variables fro
3131

3232
I also published my own templates [here](https://github.com/mProjectsCode/obsidian-media-db-templates).
3333

34+
#### Metadata field customization
35+
Allows you to rename the metadata fields this plugin generates through mappings.
36+
37+
A mapping has to follow this syntax `[origional property name] -> [new property name]`.
38+
Multiple mappings are separated by a new line.
39+
So e.g.:
40+
```
41+
title -> name
42+
year -> releaseYear
43+
```
44+
3445
### How to install
3546
**The plugin is now released, so it can be installed directly through obsidian's plugin installer.**
3647

@@ -46,8 +57,7 @@ The folder structure should look like this:
4657
|_ main.js
4758
|_ manifest.json
4859
|_ styles.css
49-
```
50-
60+
```
5161

5262

5363
### How to use
@@ -102,6 +112,11 @@ Now you select the result you want and the plugin will cast it's magic and creat
102112
### Problems, unexpected behavior or improvement suggestions?
103113
You are more than welcome to open an issue on [GitHub](https://github.com/mProjectsCode/obsidian-media-db-plugin/issues).
104114

115+
### Changelog
116+
#### 0.2.0
117+
- Added the option to rename metadata fields through property mappings
118+
- fixed note creation falling, when the folder set in the settings did not exist
119+
105120
### Contributions
106121
Thank you for wanting to contribute to this project.
107122

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "obsidian-media-db-plugin",
33
"name": "Media DB Plugin",
4-
"version": "0.1.11",
4+
"version": "0.2.0",
55
"minAppVersion": "0.14.0",
66
"description": "A plugin that can query multiple APIs for movies, series, anime, games, music and wiki articles, and import them into your vault. ",
77
"author": "Moritz Jung",

src/api/apis/SteamAPI.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ export class SteamAPI extends APIModel {
8181

8282
let result;
8383
for (const [key, value] of Object.entries(await fetchData.json)) {
84-
if (key == id) {
84+
// console.log(typeof key, key)
85+
// console.log(typeof id, id)
86+
// after some testing I found out that id is somehow a number despite that it's defined as string...
87+
if (key === String(id)) {
8588
result = value.data;
8689
}
8790
}

src/main.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ import {WikipediaAPI} from './api/apis/WikipediaAPI';
1212
import {MusicBrainzAPI} from './api/apis/MusicBrainzAPI';
1313
import {MediaTypeManager} from './utils/MediaTypeManager';
1414
import {SteamAPI} from './api/apis/SteamAPI';
15+
import {ModelPropertyMapper} from './settings/ModelPropertyMapper';
16+
import {YAMLConverter} from './utils/YAMLConverter';
1517

1618
export default class MediaDbPlugin extends Plugin {
1719
settings: MediaDbPluginSettings;
1820
apiManager: APIManager;
1921
mediaTypeManager: MediaTypeManager;
22+
modelPropertyMapper: ModelPropertyMapper;
2023

2124
async onload() {
2225
await this.loadSettings();
@@ -68,6 +71,7 @@ export default class MediaDbPlugin extends Plugin {
6871
// this.apiManager.registerAPI(new LocGovAPI(this)); // TODO: parse data
6972

7073
this.mediaTypeManager = new MediaTypeManager(this.settings);
74+
this.modelPropertyMapper = new ModelPropertyMapper(this.settings);
7175
}
7276

7377
async createMediaDbNote(modal: () => Promise<MediaTypeModel>): Promise<void> {
@@ -87,7 +91,7 @@ export default class MediaDbPlugin extends Plugin {
8791
console.log('MDB | Creating new note...');
8892
// console.log(mediaTypeModel);
8993

90-
let fileContent = `---\n${mediaTypeModel.toMetaData()}---\n`;
94+
let fileContent = `---\n${YAMLConverter.toYaml(this.modelPropertyMapper.convertObject(mediaTypeModel.toMetaDataObject()))}---\n`;
9195

9296
if (this.settings.templates) {
9397
fileContent += await this.mediaTypeManager.getContent(mediaTypeModel, this.app);
@@ -96,6 +100,11 @@ export default class MediaDbPlugin extends Plugin {
96100
const fileName = replaceIllegalFileNameCharactersInString(this.mediaTypeManager.getFileName(mediaTypeModel));
97101
const filePath = `${this.settings.folder.replace(/\/$/, '')}/${fileName}.md`;
98102

103+
const folder = this.app.vault.getAbstractFileByPath(this.settings.folder);
104+
if (!folder) {
105+
await this.app.vault.createFolder(this.settings.folder.replace(/\/$/, ''));
106+
}
107+
99108
const file = this.app.vault.getAbstractFileByPath(filePath);
100109
if (file) {
101110
await this.app.vault.delete(file);
@@ -144,13 +153,17 @@ export default class MediaDbPlugin extends Plugin {
144153
throw new Error('MDB | there is no active note');
145154
}
146155

147-
let metadata: FrontMatterCache = this.app.metadataCache.getFileCache(activeFile).frontmatter;
156+
let metadata: any = this.app.metadataCache.getFileCache(activeFile).frontmatter;
157+
delete metadata.position; // remove unnecessary data from the FrontMatterCache
158+
metadata = this.modelPropertyMapper.convertObjectBack(metadata);
159+
160+
console.log(metadata)
148161

149162
if (!metadata?.type || !metadata?.dataSource || !metadata?.id) {
150163
throw new Error('MDB | active note is not a Media DB entry or is missing metadata');
151164
}
152165

153-
delete metadata.position; // remove unnecessary data from the FrontMatterCache
166+
154167
let oldMediaTypeModel = this.mediaTypeManager.createMediaTypeModelFromMediaType(metadata, metadata.type);
155168

156169
let newMediaTypeModel = await this.apiManager.queryDetailedInfoById(metadata.id, metadata.dataSource);
@@ -171,6 +184,8 @@ export default class MediaDbPlugin extends Plugin {
171184

172185
async saveSettings() {
173186
this.mediaTypeManager.updateTemplates(this.settings);
187+
this.modelPropertyMapper.updateConversionRules(this.settings);
188+
174189
await this.saveData(this.settings);
175190
}
176191
}

src/models/MediaTypeModel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export abstract class MediaTypeModel {
1717

1818
abstract getTags(): string[];
1919

20-
toMetaData(): string {
21-
return YAMLConverter.toYaml({...this.getWithOutUserData(), ...this.userData, tags: '#' + this.getTags().join('/')});
20+
toMetaDataObject(): object {
21+
return {...this.getWithOutUserData(), ...this.userData, tags: '#' + this.getTags().join('/')};
2222
}
2323

2424
getWithOutUserData(): object {

src/models/WikiModel.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class WikiModel extends MediaTypeModel {
1616
wikiUrl: string;
1717
lastUpdated: string;
1818
length: number;
19+
article: string;
1920

2021
userData: {};
2122

@@ -35,4 +36,11 @@ export class WikiModel extends MediaTypeModel {
3536
return MediaType.Wiki;
3637
}
3738

39+
override getWithOutUserData(): object {
40+
const copy = JSON.parse(JSON.stringify(this));
41+
delete copy.userData;
42+
delete copy.article;
43+
return copy;
44+
}
45+
3846
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {containsOnlyLettersAndUnderscores} from '../utils/Utils';
2+
3+
export class ModelPropertyConversionRule {
4+
property: string;
5+
newProperty: string;
6+
7+
constructor(conversionRule: string) {
8+
const conversionRuleParts = conversionRule.split('->');
9+
if (conversionRuleParts.length !== 2) {
10+
throw Error(`Conversion rule "${conversionRule}" may only have exactly one "->"`);
11+
}
12+
13+
let property = conversionRuleParts[0].trim();
14+
let newProperty = conversionRuleParts[1].trim();
15+
16+
if (!property || !containsOnlyLettersAndUnderscores(property)) {
17+
throw Error(`Error in conversion rule "${conversionRule}": property may not be empty and only contain letters and underscores.`);
18+
}
19+
20+
if (!newProperty || !containsOnlyLettersAndUnderscores(newProperty)) {
21+
throw Error(`Error in conversion rule "${conversionRule}": new property may not be empty and only contain letters and underscores.`);
22+
}
23+
24+
this.property = property;
25+
this.newProperty = newProperty;
26+
}
27+
}

0 commit comments

Comments
 (0)