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

Introduce first tests in the codebase #36

Merged
merged 1 commit into from
Oct 16, 2022
Merged
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
6 changes: 6 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript'
]
};
16,631 changes: 11,850 additions & 4,781 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"bump": "node version-bump.mjs",
"bump:beta": "node version-beta.mjs",
"beta": "commit-and-tag-version --prerelease --tag-build beta --skip.changelog && node version-beta.mjs && git add manifest-beta.json && git commit --amend --no-edit",
"publish": "commit-and-tag-version --packageFiles package.json --bumpFiles manifest.json package.json"
"publish": "commit-and-tag-version --packageFiles package.json --bumpFiles manifest.json package.json",
"test": "jest"
},
"commit-and-tag-version": {
"t": "",
Expand All @@ -32,18 +33,24 @@
"author": "Mara",
"license": "AGPL-3.0",
"devDependencies": {
"@babel/core": "^7.19.3",
"@babel/preset-env": "^7.19.4",
"@babel/preset-typescript": "^7.18.6",
"@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "^5.29.0",
"@typescript-eslint/parser": "^5.29.0",
"babel-jest": "^29.2.0",
"builtin-modules": "^3.3.0",
"esbuild": "0.14.47",
"eslint": "^7.32.0",
"eslint-config-standard": "^16.0.3",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.2.0",
"jest": "^29.2.0",
"obsidian": "github:obsidianmd/obsidian-api",
"prettier": "^2.6.2",
"ts-jest": "^29.0.3",
"tslib": "2.4.0",
"typescript": "4.7.4"
},
Expand Down
15 changes: 0 additions & 15 deletions plugin/contents_conversion/convertText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,6 @@ export async function addInlineTags(
return text;
}

export function censorText(text: string, settings: GitHubPublisherSettings): string {
/*
* Censor text using the settings
*/
if (!settings.censorText) {
return text;
}
for (const censor of settings.censorText) {
const regex = new RegExp(censor.entry, 'ig');
// @ts-ignore
text = text.replaceAll(regex, censor.replace);
}
return text;
}

function dataviewExtract(fieldValue: Link, settings: GitHubPublisherSettings) {
/*
* stringify the dataview link by extracting the value from the link
Expand Down
21 changes: 21 additions & 0 deletions plugin/contents_conversion/findAndReplaceText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {GitHubPublisherSettings} from "../settings/interface";

/**
* Given a series of `censor` entries in Settings, this will loop through each
* then find and replace.
*/
export default function findAndReplaceText(text: string, settings: GitHubPublisherSettings): string {
/*
* Censor text using the settings
*/
if (!settings.censorText) {
return text;
}
for (const censor of settings.censorText) {
const regex = new RegExp(censor.entry, 'ig');
// @ts-ignore
text = text.replaceAll(regex, censor.replace);
}
return text;
}

6 changes: 4 additions & 2 deletions plugin/publishing/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import {
import {
convertDataviewQueries,
addHardLineBreak,
censorText, addInlineTags, convertInlineDataview
addInlineTags, convertInlineDataview
} from "../contents_conversion/convertText";

import findAndReplaceText from "../contents_conversion/findAndReplaceText";

import {
getReceiptFolder, getImageLinkOptions
} from "../contents_conversion/filePathConvertor";
Expand Down Expand Up @@ -122,7 +124,7 @@ export default class Publisher {
text = addHardLineBreak(text, this.settings, frontmatterSettings);
text = convertLinkCitation(text, this.settings, linkedFiles, this.metadataCache, file, this.vault, frontmatter);
text = convertWikilinks(text, frontmatterSettings, this.settings, linkedFiles);
text = censorText(text, this.settings);
text = findAndReplaceText(text, this.settings);
const path = getReceiptFolder(file, this.settings, this.metadataCache, this.vault)
noticeLog(`Upload ${file.name}:${path} on ${this.settings.githubName}/${this.settings.githubRepo}:${ref}`, this.settings);
await this.uploadText(file.path, text, path, file.name, ref);
Expand Down
42 changes: 42 additions & 0 deletions tests/fixtures/githubPublisherSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { GitHubPublisherSettings } from './../../plugin/settings/interface';

const settings: GitHubPublisherSettings = {
githubRepo: "",
githubName: "",
GhToken: "",
githubBranch: "",
shareKey: "",
ExcludedFolder: "",
fileMenu: false,
editorMenu: false,
downloadedFolder: "",
folderDefaultName: "",
yamlFolderKey: "",
rootFolder: "",
workflowName: "",
embedImage: false,
defaultImageFolder: "",
autoCleanUp: false,
autoCleanUpExcluded: "",
folderNote: false,
convertWikiLinks: false,
convertForGithub: false,
subFolder: "",
embedNotes: false,
copyLink: false,
mainLink: "",
linkRemover: "",
hardBreak: false,
logNotice: false,
convertDataview: false,
useFrontmatterTitle: false,
censorText: [],
inlineTags: false,
dataviewFields: [""],
frontmatterTitleKey: "",
excludeDataviewValue: [""],
metadataFileFields: [""],
shareExternalModified: false
}

export default settings;
151 changes: 151 additions & 0 deletions tests/plugin/convertText.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { beforeEach, describe, expect, test } from '@jest/globals';
import settingsFixture from "../fixtures/githubPublisherSettings";
import { GitHubPublisherSettings } from "../../plugin/settings/interface";
import findAndReplaceText from './../../plugin/contents_conversion/findAndReplaceText';

let settings: GitHubPublisherSettings;

const resetFixtures = () => {
settings = settingsFixture
}

beforeEach(() => {
resetFixtures()
});

describe('findAndReplaceText standard behavior', () => {
test('skips if settings.censorText is empty', () => {
const initialText = `file v1`;
const expectedText = `file v1`;

settings["censorText"] = []
const subject = findAndReplaceText(initialText, settings);

expect(subject).toBe(expectedText);
});

test('replaces patterns in a string in sequence', () => {
const initialText = `file v1`;
const expectedText = `file v4`;

settings["censorText"] = [
{
entry: "file v1",
replace: "file v2"
},
{
entry: "file v2",
replace: "file v3"
},
{
entry: "file v3",
replace: "file v4"
},
]
const subject = findAndReplaceText(initialText, settings);

expect(subject).toBe(expectedText);
});
Mara-Li marked this conversation as resolved.
Show resolved Hide resolved
});

describe('findAndReplaceText with patterns for Jekyll', () => {
test('replaces strings', () => {
const initialText = `
[Alt Text](http://do-not-touch-websites.com)
[Alt Text](pure-file)
[Alt Text](directory/and-file)
[Alt Text](directory/and-file-with-extension.md)
[Alt Text](./file-with-period)
[Alt Text](./directory/file-with-period)

[Alt Text](http://do-not-touch-websites.com)
[Alt Text](images/do-not-add-link-when-starting-with-image-keyword)
[Alt Text](/images/do-not-add-link-when-starting-with-image-keyword)
![Alt Text](/images/do-not-add-link-when-starting-with-image-keyword)
[Alt Text](obsidian/images/do-not-add-link-when-starting-with-obsidian-image-keyword)

\`[Alt Text](do-not-touch-code-examples)\`
\` [Alt Text](touch-code-examples with space)\`
\` [Alt Text](touch-code-examples with space) \`
\`[Alt Text]({% link obsidian/touch-code-examples with space.md %})\`
`;
const expectedText = `
[Alt Text](http://do-not-touch-websites.com)
[Alt Text]({% link obsidian/pure-file.md %})
[Alt Text]({% link obsidian/directory/and-file.md %})
[Alt Text]({% link obsidian/directory/and-file-with-extension.md %})
[Alt Text]({% link obsidian/file-with-period.md %})
[Alt Text]({% link obsidian/directory/file-with-period.md %})

[Alt Text](http://do-not-touch-websites.com)
[Alt Text](/images/do-not-add-link-when-starting-with-image-keyword)
[Alt Text](/images/do-not-add-link-when-starting-with-image-keyword)
![Alt Text](/images/do-not-add-link-when-starting-with-image-keyword)
[Alt Text](/images/do-not-add-link-when-starting-with-obsidian-image-keyword)

\`[Alt Text](do-not-touch-code-examples)\`
\` [Alt Text]({% link obsidian/touch-code-examples with space.md %})\`
\` [Alt Text]({% link obsidian/touch-code-examples with space.md %}) \`
\`[Alt Text]({% link obsidian/touch-code-examples with space.md %})\`
`;

settings["censorText"] = [
{
// Converts [a](b) into [a]({% link obsidian/b.md %})
//
// Ignore .md extensions, if any.
// We will add an .md extension later. ----------------------------------------------\
// |
// Matches the final destination |
// so we can reuse in `replace`. ----------------------------------------------\ |
// | |
// Ignores ./ so it's removed. | |
// The * makes sure it's zero | |
// or more ./ ---------------------------------------------------------\ | |
// | | |
// Skips http and image paths. | | |
// ?! stands for negative | | |
// lookahead. If `http` is | | |
// found, halts process. -------------\ | | |
// | | | |
// Starts link | | | |
// destination. -------------------\ | | | |
// | | | | |
// The link's Alt ----------\ | | | | |
// | | | | | |
// Skip links within code | | | | | |
// samples. (?<!`) makes | | | | | |
// sure no ` (e.g backtick) | | | | | |
// exists before | | | | | |
// bracket. --------\ | | | | | |
// | | | | | | |
// V V V V V V V
entry: String.raw `(?<!\`)\[(.*?)\]\((?!(http|\/*image|obsidian\/image))(\.\/)*(.+?)(\.md)*\)`,
replace: "[$1]({% link obsidian/$4.md %})"
}, {
// Converts [a](obsidian/images/b) into [a](/images/b)
//
// Everything else. -------------------------------------\
// |
// Ignores `obsidian/` prefix if any. -\ |
// | |
// Starts link | |
// destination. --------------------\ | |
// | | |
// The link's Alt ----------\ | | |
// | | | |
// Skip images within code. | | | |
// (?<!`) makes sure no | | | |
// backtick exists before | | | |
// bracket. --------\ | | | |
// | | | | |
// V V V V V
entry: String.raw `(?<!\`)\[(.*?)\]\(((obsidian\/)?image)(.+)\)`,
replace: "[$1](/image$4)"
}
]
const subject = findAndReplaceText(initialText, settings);

expect(subject).toBe(expectedText);
});
});