diff --git a/README.md b/README.md index d6bb107..6f0e863 100644 --- a/README.md +++ b/README.md @@ -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
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.
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.
Check [this](https://github.com/SilentVoid13/Chesser/tree/master/assets/board-css) to view available styles. | Style of the chess board. | @@ -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 @@ -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). \ No newline at end of file +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). diff --git a/src/Chesser.ts b/src/Chesser.ts index d7d71cc..26c9528 100644 --- a/src/Chesser.ts +++ b/src/Chesser.ts @@ -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"; @@ -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); @@ -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 "" + } + } }