Skip to content

Commit

Permalink
Merge pull request mjmlio#47 from ivank/mjml-config-path
Browse files Browse the repository at this point in the history
Add a setting for custom config files
  • Loading branch information
iRyusa authored Aug 28, 2024
2 parents 01ae0fa + b937125 commit 0ffe955
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ The following command is available:
| `mjml.templateGallery` | `false` | Show the template gallery instead of quick pick. |
| `mjml.templateGalleryAutoClose` | `true` | Automatically close template gallery when selecting a template. |
| `mjml.switchOnSeparateFileChange` | `true` | Automatically switch previews when editing a different file. |
| `mjml.mjmlConfigPath` | ` ` | The path or directory of the .mjmlconfig (or .mjmlconfig.js) file (for custom components use) |


## Snippets
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@
"default": true,
"description": "Automatically switch previews when editing a different file.",
"type": "boolean"
},
"mjml.mjmlConfigPath": {
"default": "",
"description": "The path or directory of the .mjmlconfig (or .mjmlconfig.js) file (for custom components use)",
"type": "string"
}
}
},
Expand Down
14 changes: 11 additions & 3 deletions src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync, readFileSync, statSync } from 'fs'
import { basename, dirname, join as joinPath, parse as parsePath } from 'path'
import { basename, dirname, join as joinPath, parse as parsePath, isAbsolute } from 'path'
import { TextDocument, TextEditor, window, workspace } from 'vscode'

import { html as jsBeautify } from 'js-beautify'
Expand Down Expand Up @@ -27,6 +27,9 @@ export function renderMJML(
activeTextEditor.document.getText(),
minify !== undefined ? minify : workspace.getConfiguration('mjml').minifyHtmlOutput,
beautify !== undefined ? beautify : workspace.getConfiguration('mjml').beautifyHtmlOutput,
undefined,
'skip',
workspace.getConfiguration('mjml').mjmlConfigPath,
).html

if (content) {
Expand All @@ -53,6 +56,7 @@ export function mjmlToHtml(
beautify: boolean,
path?: string,
validation: 'strict' | 'soft' | 'skip' = 'skip',
mjmlConfigPath?: string,
): { html: string; errors: any[] } {
try {
if (!path) {
Expand All @@ -63,7 +67,11 @@ export function mjmlToHtml(
beautify,
filePath: path,
minify,
mjmlConfigPath: getCWD(path),
mjmlConfigPath: mjmlConfigPath
? isAbsolute(mjmlConfigPath)
? mjmlConfigPath
: joinPath(getCWD(path), mjmlConfigPath)
: getCWD(path),
validationLevel: validation,
})
} catch (error) {
Expand Down Expand Up @@ -107,7 +115,7 @@ export function beautifyHTML(mjml: string): string | undefined {

return beautified
} catch (error) {
window.showErrorMessage(error)
window.showErrorMessage(error as string)

return
}
Expand Down
1 change: 1 addition & 0 deletions src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default class Linter {
false,
getPath(),
'strict',
workspace.getConfiguration('mjml').mjmlConfigPath
).errors

if (errors && errors[0]) {
Expand Down
1 change: 1 addition & 0 deletions src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export default class Preview {
false,
document.uri.fsPath,
'skip',
workspace.getConfiguration('mjml').mjmlConfigPath
).html

if (html) {
Expand Down

0 comments on commit 0ffe955

Please sign in to comment.