Skip to content

Commit

Permalink
🐛 fix redundant $$ symbols in some kind of formulas in .html
Browse files Browse the repository at this point in the history
  • Loading branch information
mokeyish committed May 17, 2022
1 parent 8858fa4 commit 89fd0b8
Show file tree
Hide file tree
Showing 16 changed files with 2,094 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/dist
/node_modules
/coverage
/lua/main.lua
/.env.local
/.idea
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node'
};
25 changes: 25 additions & 0 deletions lua/math_block.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@



function Para(el)
local content = {}
local in_display_math = false
for _, item in pairs(el.content) do
if item.t == 'Str'and item.text == "$$" then
in_display_math = not in_display_math
else
if in_display_math then
if item.t == 'RawInline' and item.format == 'tex' then
local n = pandoc.Math('DisplayMath', '\n' .. item.text .. '\n')
table.insert(content, n)
elseif (item.t ~= 'SoftBreak') then
table.insert(content, item)
end
else
table.insert(content, item)
end
end
end
el.content = content
return el
end
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-enhancing-export",
"name": "Obsidian Enhancing Export",
"version": "1.0.7",
"version": "1.0.8",
"minAppVersion": "0.12.0",
"description": "This is a enhancing export plugin for Obsidian. It allows to export to formats like Html, DOCX, ePub and PDF or Markdown(Hugo) etc.",
"author": "YISH",
Expand Down
62 changes: 33 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
{
"name": "obsidian-enhancing-export",
"version": "1.0.7",
"description": "This is a enhancing export plugin for Obsidian. It allows to export to formats like Html, DOCX, ePub and PDF or Markdown(Hugo) etc.",
"main": "dist/main.js",
"scripts": {
"dev": "node esbuild.config.mjs",
"build": "node esbuild.config.mjs production",
"name": "obsidian-enhancing-export",
"version": "1.0.8",
"description": "This is a enhancing export plugin for Obsidian. It allows to export to formats like Html, DOCX, ePub and PDF or Markdown(Hugo) etc.",
"main": "dist/main.js",
"scripts": {
"dev": "node esbuild.config.mjs",
"build": "node esbuild.config.mjs production",
"version": "node version-bump.mjs && git add manifest.json versions.json",
"lint": "eslint --ext .ts,.js src build",
"lint-fix": "eslint --fix --ext .ts,.js src build gulpfile.js",
"format-check": "prettier --check \"src/**/*.ts\"",
"format-fix": "prettier --write \"src/**/*.ts\""
},
"keywords": [],
"author": "YISH",
"license": "MIT",
"lint": "eslint --ext .ts,.js src build",
"lint-fix": "eslint --fix --ext \"src/**/*.ts\"",
"format-check": "prettier --check \"src/**/*.ts\"",
"format-fix": "prettier --write \"src/**/*.ts\"",
"test": "jest"
},
"keywords": [],
"author": "YISH",
"license": "MIT",
"repository": "https://github.com/mokeyish/obsidian-enhancing-export",
"devDependencies": {
"@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0",
"builtin-modules": "^3.2.0",
"dotenv": "^16.0.0",
"esbuild": "0.14.34",
"esbuild-plugin-copy": "^1.3.0",
"eslint": "^8.13.0",
"obsidian": "latest",
"prettier": "^2.6.2",
"tslib": "2.3.1",
"typescript": "4.6.3"
}
"devDependencies": {
"@types/jest": "^27.5.1",
"@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0",
"builtin-modules": "^3.2.0",
"dotenv": "^16.0.0",
"esbuild": "0.14.34",
"esbuild-plugin-copy": "^1.3.0",
"eslint": "^8.13.0",
"jest": "^28.1.0",
"obsidian": "latest",
"prettier": "^2.6.2",
"ts-jest": "^28.0.2",
"tslib": "2.3.1",
"typescript": "4.6.3"
}
}
6 changes: 3 additions & 3 deletions src/export_command_templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ export default {
name: 'Markdown',
type: 'pandoc',
arguments:
'-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${pluginDir}/lua/markdown.lua" -s -o "${outputPath}" -t commonmark_x-attributes',
'-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/markdown.lua" -s -o "${outputPath}" -t commonmark_x-attributes',
extension: '.md',
},
'Markdown (Hugo)': {
name: 'Markdown (Hugo)',
type: 'pandoc',
arguments:
'-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${pluginDir}/lua/markdown+hugo.lua" -s -o "${outputPath}" -t commonmark_x-attributes',
'-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/markdown+hugo.lua" -s -o "${outputPath}" -t commonmark_x-attributes',
extension: '.md',
},
'Html': {
name: 'Html',
type: 'pandoc',
arguments:
'-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --self-contained --metadata title="${currentFileName}" -s -o "${outputPath}" -t html',
'-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/math_block.lua" --self-contained --metadata title="${currentFileName}" -s -o "${outputPath}" -t html',
customArguments: '--mathjax="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg-full.js"',
extension: '.html',
},
Expand Down
5 changes: 4 additions & 1 deletion src/exporto0o.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Notice, TFile } from 'obsidian';
import * as fs from 'fs';
import type ExportPlugin from './main';
import { exec } from 'child_process';
import path from 'path';

export async function exportToOo(
plugin: ExportPlugin,
Expand Down Expand Up @@ -50,6 +51,7 @@ export async function exportToOo(
*/
const vaultDir = adapter.getBasePath();
const pluginDir = `${vaultDir}/${manifest.dir}`;
const luaDir = `${pluginDir}/lua`;
const outputDir = candidateOutputDirectory;
const outputPath = `${outputDir}/${candidateOutputFileName}`;
const outputFileName = candidateOutputFileName.substring(0, candidateOutputFileName.lastIndexOf('.'));
Expand All @@ -64,11 +66,12 @@ export async function exportToOo(
if (attachmentFolderPath === '/') {
attachmentFolderPath = vaultDir;
} else if (attachmentFolderPath.startsWith('.')) {
attachmentFolderPath = `${currentDir}/${attachmentFolderPath.substring(1)}`;
attachmentFolderPath = path.join(currentDir, attachmentFolderPath.substring(1));
}

const variables: Variables = {
pluginDir,
luaDir,
outputDir,
outputPath,
outputFileName,
Expand Down
2 changes: 2 additions & 0 deletions src/lua.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import utf8_filenames from 'lua/utf8_filenames.lua';
import url from 'lua/url.lua';
import polyfill from 'lua/polyfill.lua';
import math_block from 'lua/math_block.lua';
import markdown from 'lua/markdown.lua';
import markdown_hugo from 'lua/markdown+hugo.lua';

const files = {
'utf8_filenames.lua': utf8_filenames,
'url.lua': url,
'polyfill.lua': polyfill,
'math_block.lua': math_block,
'markdown.lua': markdown,
'markdown+hugo.lua': markdown_hugo,
};
Expand Down
1 change: 1 addition & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import export_command_templates from './export_command_templates';
export interface Variables {
attachmentFolderPath: string;
pluginDir: string;
luaDir: string;
outputDir: string;
outputPath: string;
outputFileName: string;
Expand Down
24 changes: 24 additions & 0 deletions tests/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { exec as execSync, ExecException } from 'child_process';
import os from 'os';


export async function exec(cmd: string, options: { lineSeparator: '\n' | '\r\n' | '\r' }): Promise<string> {
function lineSeparator(s?: string, ls?: '\n' | '\r\n' | '\r') {
if (!s || os.EOL === ls || !ls) {
return s;
}
return s.replaceAll(os.EOL, ls);
}
return await new Promise((resolve, reject) => {
execSync(cmd, { encoding: 'utf-8', cwd: module.path }, (e: ExecException, stdout: string, stderr: string) => {
if (!e) {
resolve(lineSeparator(stdout, options?.lineSeparator));
} else {
reject(lineSeparator(stderr, options?.lineSeparator));
}
});
});
}



13 changes: 13 additions & 0 deletions tests/markdowns/math-block.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
$$
\begin{align*}
\begin{rcases}
\lambda_{1}(a_{11}^{*}, \ldots, a_{22}^{*}) < 0 \\
\lambda_{2}(a_{11}^{*}, \ldots, a_{22}^{*}) < 0 \\
\end{rcases} & \Rightarrow \text{stable knot} \\
\begin{rcases}
\lambda_{1}(a_{11}^{*}, \ldots, a_{22}^{*}) > 0 \\
\lambda_{2}(a_{11}^{*}, \ldots, a_{22}^{*}) < 0 \\
\end{rcases} & \Rightarrow \text{saddle}
\end{align*}
$$
8 changes: 8 additions & 0 deletions tests/markdowns/math-block.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Pandoc
Meta { unMeta = fromList [] }
[ Para
[ Math
DisplayMath
"\n\\begin{align*}\n\\begin{rcases}\n\\lambda_{1}(a_{11}^{*}, \\ldots, a_{22}^{*}) < 0 \\\\\n\\lambda_{2}(a_{11}^{*}, \\ldots, a_{22}^{*}) < 0 \\\\\n\\end{rcases} & \\Rightarrow \\text{stable knot} \\\\\n\n \\begin{rcases}\n \\lambda_{1}(a_{11}^{*}, \\ldots, a_{22}^{*}) > 0 \\\\\n \\lambda_{2}(a_{11}^{*}, \\ldots, a_{22}^{*}) < 0 \\\\\n \\end{rcases} & \\Rightarrow \\text{saddle}\n\\end{align*}\n"
]
]
14 changes: 14 additions & 0 deletions tests/mathBlock.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { exec } from './common';
import { readFile } from 'fs/promises';


test('test math block parsing', async () => {
process.chdir(module.path);
const input_file = './markdowns/math-block.md';
const expect_out = './markdowns/math-block.out';
const lua_script = '../lua/math_block.lua';
const pandoc = `pandoc -s -L ${lua_script} -t native -f markdown "${input_file}"`;
const ret = await exec(pandoc, { lineSeparator: '\n'});
// await writeFile('./out.txt', ret);
expect(ret).toBe(await readFile(expect_out, { encoding: 'utf-8', flag: 'r' }));
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"allowJs": true,
"noImplicitAny": true,
"moduleResolution": "node",
"esModuleInterop": true,
"importHelpers": true,
"lib": [
"DOM",
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"1.0.4": "0.12.0",
"1.0.5": "0.12.0",
"1.0.6": "0.12.0",
"1.0.7": "0.12.0"
"1.0.7": "0.12.0",
"1.0.8": "0.12.0"
}
Loading

0 comments on commit 89fd0b8

Please sign in to comment.