Skip to content

Commit

Permalink
Merge pull request #19 from saberzero1/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
saberzero1 authored Oct 10, 2024
2 parents 032f8fc + d265d0e commit 88ac179
Show file tree
Hide file tree
Showing 14 changed files with 272 additions and 189 deletions.
76 changes: 38 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
# Obsidian Quartz Syncer

Parse, upload and sync your Obsidian notes to your [Quartz](https://github.com/jackyzha0/quartz) website.

**This plugin assumes you have set up a Quartz repository.**

## Initial Setup

Install the plugin by downloading it from the <a href="https://github.com/saberzero1/quartz-syncer/releases">Release Tab</a>, or through the <a href="https://github.com/TfTHacker/obsidian42-brat">Obsidian42 Brat plugin</a>.

After installing, open the plugin settings in Obsidian and set your Github Username, the name of your fork of [Quartz](https://github.com/jackyzha0/quartz), and the authentication token.

Don't have an authentication token yet? You can generate it <a href="https://github.com/settings/tokens/new?scopes=repo">here</a>.

## Publishing notes to Quartz

You can click on the icon in the sidebar or launch the `Quartz Syncer: Open Publication Center` to publish/unpublish notes.

> [!IMPORTANT]
> Notes need a `publish` flag in the notes <a href="https://help.obsidian.md/Editing+and+formatting/Properties">frontmatter</a>. You can add these with the `Quartz Syncer: Add Publish Flag` command, or by adding them to the notes properties manually or via template.
The following frontmatter in a note will cause it to be published when opening the Publication Center:

```yaml
publish: true
```
The following frontmatter in a note will cause it to be shown in the Publication Center, but not published.
```yaml
publish: false
```
The notes will appear in the `content` folder in your chosen repository.

## Credits

This repository is a modified version of <a href="https://github.com/oleeskild">Ole's</a> <a href="https://github.com/oleeskild/obsidian-digital-garden">Digital Garden plugin</a>. If you found this project useful and want to support it, please support him instead: <a href="https://ko-fi.com/oleeskild">Donation link</a>.
# Obsidian Quartz Syncer

Parse, upload and sync your Obsidian notes to your [Quartz](https://github.com/jackyzha0/quartz) website.

**This plugin assumes you have set up a Quartz repository.**

## Initial Setup

Install the plugin by downloading it from the <a href="https://github.com/saberzero1/quartz-syncer/releases">Release Tab</a>, or through the <a href="https://github.com/TfTHacker/obsidian42-brat">Obsidian42 Brat plugin</a>.

After installing, open the plugin settings in Obsidian and set your Github Username, the name of your fork of [Quartz](https://github.com/jackyzha0/quartz), and the authentication token.

Don't have an authentication token yet? You can generate it <a href="https://github.com/settings/tokens/new?scopes=repo">here</a>.

## Publishing notes to Quartz

You can click on the icon in the sidebar or launch the `Quartz Syncer: Open Publication Center` to publish/unpublish notes.

> [!IMPORTANT]
> Notes need a `publish` flag in the notes <a href="https://help.obsidian.md/Editing+and+formatting/Properties">frontmatter</a>. You can add these with the `Quartz Syncer: Add Publish Flag` command, or by adding them to the notes properties manually or via template.
The following frontmatter in a note will cause it to be published when opening the Publication Center:

```yaml
publish: true
```
The following frontmatter in a note will cause it to be shown in the Publication Center, but not published.
```yaml
publish: false
```
The notes will appear in the `content` folder in your chosen repository.

## Credits

This repository is a modified version of <a href="https://github.com/oleeskild">Ole's</a> <a href="https://github.com/oleeskild/obsidian-digital-garden">Digital Garden plugin</a>. If you found this project useful and want to support it, please support him instead: <a href="https://ko-fi.com/oleeskild">Donation link</a>.
26 changes: 26 additions & 0 deletions content/015 Code blocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
dg-publish: true
---
These codeblocks should not be modified upon publish.

Sample 1
```jinja2
{{ highlight_text }}{% if highlight_location and highlight_location_url %} ([via]({{highlight_location_url}})){% elif highlight_location %} ({{highlight_location}}){% endif %} ^rwhi{{highlight_id}}
{% if highlight_note %}
{{ highlight_note }} ^rwhi{{highlight_id}}-note
{% endif %}
```

Sample 2
```md
In medieval Latin a florilegium (plural florilegia) was a compilation of excerpts from other writings.
The word is from the Latin flos (flower) and legere (to gather): literally a gathering of flowers, or collection of fine extracts from the body of a larger work. ([via](https://en.wikipedia.org/wiki/Florilegium)) ^rwhi724352030
```

Sample 3
```
This codeblock has a transclusion syntax in it.
Check it out: ![[001 Links]]
```

And for sanity, here's some block references outside of code blocks: foobar ^test-123
2 changes: 1 addition & 1 deletion esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ esbuild.build({
bundle: true,
external: ['obsidian', 'electron', ...builtins],
format: 'cjs',
target: 'es2016',
target: 'es2018',
logLevel: "info",
sourcemap: prod ? false : 'inline',
treeShaking: true,
Expand Down
28 changes: 15 additions & 13 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Notice, Platform, Plugin, Workspace, addIcon } from "obsidian";
import Publisher from "./src/publisher/Publisher";
import QuartzSyncerSettings from "./src/models/settings";
import { bookHeart } from "./src/ui/suggest/constants";
import { PublishStatusBar } from "./src/views/PublishStatusBar";
//import { PublishStatusBar } from "./src/views/PublishStatusBar";
import { PublicationCenter } from "src/views/PublicationCenter/PublicationCenter";
import PublishStatusManager from "src/publisher/PublishStatusManager";
import ObsidianFrontMatterEngine from "src/publishFile/ObsidianFrontMatterEngine";
Expand Down Expand Up @@ -65,9 +65,7 @@ export default class QuartzSyncer extends Plugin {

this.settings.logLevel && Logger.setLevel(this.settings.logLevel);

Logger.info(
"Digital garden log level set to " + Logger.getLevel().name,
);
Logger.info("Quartz Syncer log level set to " + Logger.getLevel().name);
this.addSettingTab(new QuartzSyncerSettingTab(this.app, this));

await this.addCommands();
Expand Down Expand Up @@ -98,13 +96,15 @@ export default class QuartzSyncer extends Plugin {
}

async addCommands() {
/*
this.addCommand({
id: "publish-note",
name: "Publish Single Note",
callback: async () => {
await this.publishSingleNote();
},
});
*/

if (this.settings["ENABLE_DEVELOPER_TOOLS"] && Platform.isDesktop) {
Logger.info("Developer tools enabled");
Expand All @@ -118,7 +118,7 @@ export default class QuartzSyncer extends Plugin {
import("./src/test/snapshot/generateSyncerSnapshot")
.then((snapshotGen) => {
this.addCommand({
id: "generate-garden-snapshot",
id: "generate-syncer-snapshot",
name: "Generate Syncer Snapshot",
callback: async () => {
await snapshotGen.generateSyncerSnapshot(
Expand All @@ -133,6 +133,7 @@ export default class QuartzSyncer extends Plugin {
});
}

/*
this.addCommand({
id: "publish-multiple-notes",
name: "Publish Multiple Notes",
Expand Down Expand Up @@ -197,15 +198,15 @@ export default class QuartzSyncer extends Plugin {
await publisher.publishBatch(filesToPublish);
statusBar.incrementMultiple(filesToPublish.length);
await publisher.deleteBatch(
filesToDelete.map((f) => f.path),
);
statusBar.incrementMultiple(filesToDelete.length);
for (const file of filesToDelete) {
await publisher.deleteNote(file.path);
statusBar.increment();
}
await publisher.deleteBatch(
imagesToDelete.map((f) => f.path),
);
statusBar.incrementMultiple(imagesToDelete.length);
for (const image of imagesToDelete) {
await publisher.deleteImage(image.path);
statusBar.increment();
}
statusBar.finish(8000);
Expand Down Expand Up @@ -234,6 +235,7 @@ export default class QuartzSyncer extends Plugin {
}
},
});
*/

this.addCommand({
id: "open-publish-modal",
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "obsidian-garden",
"version": "1.0.0",
"description": "A plugin used for publishing notes to a digital garden",
"name": "quartz-syncer",
"version": "1.1.7",
"description": "A plugin used for publishing notes to Quartz",
"main": "main.js",
"scripts": {
"dev": "node esbuild.config.mjs",
Expand Down
17 changes: 2 additions & 15 deletions src/compiler/SyncerPageCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
import Logger from "js-logger";
import { DataviewCompiler } from "./DataviewCompiler";
import { PublishFile } from "../publishFile/PublishFile";
import { replaceBlockIDs } from "./replaceBlockIDs";

export interface Asset {
path: string;
Expand Down Expand Up @@ -146,21 +147,7 @@ export class SyncerPageCompiler {
};

createBlockIDs: TCompilerStep = () => (text: string) => {
const block_pattern = / \^([\w\d-]+)/g;
const complex_block_pattern = /\n\^([\w\d-]+)\n/g;

text = text.replace(
complex_block_pattern,
(_match: string, $1: string) => {
return `{ #${$1}}\n\n`;
},
);

text = text.replace(block_pattern, (match: string, $1: string) => {
return `\n{ #${$1}}\n`;
});

return text;
return replaceBlockIDs(text);
};

removeObsidianComments: TCompilerStep = () => (text) => {
Expand Down
65 changes: 65 additions & 0 deletions src/compiler/createBlockIDs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { replaceBlockIDs } from "./replaceBlockIDs";

describe("replaceBlockIDs", () => {
it("should replace block IDs in markdown", () => {
const EXPECTED_MARKDOWN = `
# header
foo ^block-id-1234
bar ^block-id-5678
`;

const result = replaceBlockIDs(EXPECTED_MARKDOWN);

expect(result).toBe(`
# header
foo
{ #block-id-1234}
bar
{ #block-id-5678}
`);
});

it("should not replace block IDs in code blocks", () => {
const CODEBLOCK_WITH_BLOCKIDS = `
\`\`\`
foobar.
this is a code block.
but it contains a block ID to try to fool the compiler
and, consequently, wreck your garden.
here it goes: ^block-id-1234
and for fun, here's another: ^block-id-5678
\`\`\`
additionally, block IDs outside of code blocks should be replaced
for example, ^block-id-9999
and ^block-id-0000
`;

const result = replaceBlockIDs(CODEBLOCK_WITH_BLOCKIDS);

expect(result).toBe(`
\`\`\`
foobar.
this is a code block.
but it contains a block ID to try to fool the compiler
and, consequently, wreck your garden.
here it goes: ^block-id-1234
and for fun, here's another: ^block-id-5678
\`\`\`
additionally, block IDs outside of code blocks should be replaced
for example,
{ #block-id-9999}
and
{ #block-id-0000}
`);
});
});
35 changes: 35 additions & 0 deletions src/compiler/replaceBlockIDs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export function replaceBlockIDs(markdown: string) {
const block_pattern = / \^([\w\d-]+)/g;
const complex_block_pattern = /\n\^([\w\d-]+)\n/g;

// To ensure code blocks are not modified...
const codeBlockPattern = /```[\s\S]*?```/g;

// Extract code blocks and replace them with placeholders
const codeBlocks: string[] = [];

markdown = markdown.replace(codeBlockPattern, (match) => {
codeBlocks.push(match);

return `{{CODE_BLOCK_${codeBlocks.length - 1}}}`;
});

// Replace patterns outside code blocks
markdown = markdown.replace(
complex_block_pattern,
(_match: string, $1: string) => {
return `{ #${$1}}\n\n`;
},
);

markdown = markdown.replace(block_pattern, (_match: string, $1: string) => {
return `\n{ #${$1}}\n`;
});

// Reinsert code blocks
codeBlocks.forEach((block, index) => {
markdown = markdown.replace(`{{CODE_BLOCK_${index}}}`, block);
});

return markdown;
}
2 changes: 1 addition & 1 deletion src/publisher/Publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default class Publisher {

return await this.delete(path, sha);
}
/** If provided with sha, garden connection does not need to get it seperately! */
/** If provided with sha, syncer connection does not need to get it separately! */
public async delete(path: string, sha?: string): Promise<boolean> {
this.validateSettings();

Expand Down
Loading

0 comments on commit 88ac179

Please sign in to comment.