Skip to content
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

Pgn file support #23

Open
wants to merge 3 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
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Here are the available settings for a `chesr` code block:
| Name | Possible Values | Description |
| ------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| `fen` | A valid FEN string | Starts the chess board with a particular position. |
| `pgn` | Either, a valid PGN string formatted for YAML <br /> or the filepath to a `.pgn` file in the same folder | Loads the chess board with the moves from a PGN game |
| `orientation` | `white`/`black` | Orientation of the board. |
| `pieceStyle` | A valid piece style name.<br />Check [this](https://github.com/SilentVoid13/Chesser/tree/master/assets/piece-css) to view available styles. | Style of the pieces on the board. |
| `boardStyle` | A valid board style name.<br />Check [this](https://github.com/SilentVoid13/Chesser/tree/master/assets/board-css) to view available styles. | Style of the chess board. |
Expand All @@ -32,9 +33,23 @@ Here are the available settings for a `chesr` code block:

You can permanently set some settings in [Chesser](https://github.com/SilentVoid13/Chesser)'s obsidian plugin settings.

> If providing a raw PGN string, it must be properly formatted as a multiline YAML string (pipe to indicate multiline and 2 or 4 spaces for indent) eg;
> ```
> pgn: |
> [Event "Mar del Plata"]
> [Site "Mar del Plata ARG"]
> [Date "1960.03.30"]
>
> 1. e4 e5 2. f4 exf4 3. Nf3 g5 4. h4 g4 5. Ne5 Nf6 6. d4 d6
> 7. Nd3 Nxe4 8. Bxf4 Bg7 9. Nc3 $6 Nxc3 10. bxc3 10... c5
> 11. Be2 cxd4 12. O-O Nc6 13. Bxg4 O-O 14. Bxc8 Rxc8 15. Qg4 f5
> 16. Qg3 dxc3 17. Rae1 Kh8 18. Kh1 Rg8 19. Bxd6 Bf8 20. Be5+
> Nxe5 21. Qxe5+ Rg7 22. Rxf5 Qxh4+ 23. Kg1 Qg4 24. Rf2 Be7
> 25. Re4 Qg5 26. Qd4 Rf8 27. Re5 Rd8 28. Qe4 Qh4 29. Rf4 1-0
> ```
## TODO

- [ ] Add [PGN](https://en.wikipedia.org/wiki/Portable_Game_Notation) format support
- [ ] Test [PGN](https://en.wikipedia.org/wiki/Portable_Game_Notation) file support

## Installation

Expand All @@ -60,4 +75,4 @@ You can make a [pull request](https://github.com/SilentVoid13/Chesser/pulls) to

## Support

If you want to support me and my work, you can donate me a little something by clicking [**here**](https://www.paypal.com/donate?hosted_button_id=U2SRGAFYXT32Q).
If you want to support me and my work, you can donate me a little something by clicking [**here**](https://www.paypal.com/donate?hosted_button_id=U2SRGAFYXT32Q).
18 changes: 17 additions & 1 deletion src/Chesser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ChesserConfig, parse_user_config } from "./ChesserConfig";
import { ChesserSettings } from "./ChesserSettings";
import ChesserMenu from "./menu";

import { readFileSync } from "fs";
// To bundle all css files in styles.css with rollup
import "../assets/custom.css";
import "chessground/assets/chessground.base.css";
Expand Down Expand Up @@ -125,7 +126,11 @@ export class Chesser extends MarkdownRenderChild {

if (config.pgn) {
debug(() => console.debug("loading from pgn", config.pgn));
this.chess.load_pgn(config.pgn);
if (config.pgn.split('.').pop().toLowerCase() == 'pgn'){
this.chess.load_pgn(this.loadPgnFromFile(config.pgn))
}else{
this.chess.load_pgn(config.pgn);
}
} else if (config.fen) {
debug(() => console.debug("loading from fen", config.fen));
this.chess.load(config.fen);
Expand Down Expand Up @@ -400,4 +405,15 @@ export class Chesser extends MarkdownRenderChild {
this.cg.set({ fen: this.chess.fen(), lastMove });
this.sync_board_with_gamestate();
}

public loadPgnFromFile(filepath: string): string {
try {
const root = this.app.vault.adapter.basePath + this.app.workspace.getActiveFile().parent.path
const pgn_data = readFileSync(root + filepath, 'utf8')
return pgn_data
} catch (err){
debug( () => console.debug("failed to load PGN from file", filepath))
return ""
}
}
}