Skip to content
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ If `emdo` and `emdone` magic comments were used, it will only embed the code
block wrapped by the magic comments.

It is aware of code comment styles for Ada, Assembly, Awk, Bash, C, Clojure,
COBOL, C++, C#, CSS, D, Dart, Elm, Erlang, Elixir, Fortran, F#, Gleam, Go, Haml,
Haskell, HTML, Java, Julia, JavaScript, JSX, Kotlin, Lisp, Logo, Lua, MATLAB,
OCaml, Objective-C, Mojo, Nim, Pascal, PHP, Perl, Prolog, Python, R, Ruby, Rust,
Scala, Scheme, Sass, Shell, Solidity, SQL, Swift, Tcl, TypeScript, TSX,
VBScript, Visual Basic, Wolfram, YAML, and Zig.
COBOL, C++, C#, CSS, CSV, D, Dart, Elm, Erlang, Elixir, Fortran, F#, Gleam, Go,
Haml, Haskell, HTML, Java, Julia, JavaScript, JSON, JSX, Kotlin, Lisp, Logo,
Lua, MATLAB, OCaml, Objective-C, Mojo, Nim, Pascal, PHP, Perl, Prolog, Python,
R, Ruby, Rust, Scala, Scheme, Sass, Shell, Solidity, SQL, Swift, Tcl,
TypeScript, TSX, VBScript, Visual Basic, Wolfram, YAML, and Zig.

If you reference another Markdown file, `mdembed` will embed its contents
directly, recursively embedding its code blocks.
Expand Down
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var styles = map[string]Style{
".cpp": {LineComment: "//", BlockDo: "/*", BlockDone: "*/"}, // C++
".cs": {LineComment: "//", BlockDo: "/*", BlockDone: "*/"}, // C#
".css": {BlockDo: "/*", BlockDone: "*/"}, // CSS
".csv": {NoComment: true}, // CSV
".d": {LineComment: "//", BlockDo: "/*", BlockDone: "*/"}, // D
".dart": {LineComment: "//", BlockDo: "/*", BlockDone: "*/"}, // Dart
".elm": {LineComment: "--", BlockDo: "{-", BlockDone: "-}"}, // Elm
Expand All @@ -39,6 +40,7 @@ var styles = map[string]Style{
".java": {LineComment: "//", BlockDo: "/*", BlockDone: "*/"}, // Java
".jl": {LineComment: "#", BlockDo: "#=", BlockDone: "=#"}, // Julia
".js": {LineComment: "//", BlockDo: "/*", BlockDone: "*/"}, // JavaScript
".json": {NoComment: true}, // JSON
".jsx": {LineComment: "//", BlockDo: "/*", BlockDone: "*/"}, // JSX
".kt": {LineComment: "//", BlockDo: "/*", BlockDone: "*/"}, // Kotlin
".lisp": {LineComment: ";", BlockDo: "#|", BlockDone: "|#"}, // Lisp
Expand Down Expand Up @@ -82,6 +84,7 @@ type Style struct {
LineComment string
BlockDo string
BlockDone string
NoComment bool
}

func main() {
Expand Down Expand Up @@ -251,6 +254,14 @@ func processCodeFile(filename, blockName, fileContent string, output io.Writer)
fileName = style.LineComment + " " + filename
} else if style.BlockDo != "" && style.BlockDone != "" {
fileName = fmt.Sprintf("%s %s %s", style.BlockDo, filename, style.BlockDone)
} else {
// For file types without comments (like JSON), just use the filename
fileName = filename
}

// If a block name is specified and the file doesn't support comments, return an error
if blockName != "" && style.NoComment {
return fmt.Errorf("block embedding is not supported for files that do not support comments, such as %s", filename)
}

// If a block name is specified, extract block between marks
Expand Down Expand Up @@ -295,6 +306,7 @@ func getBlockMarkers(style Style, blockName string) (string, string) {
doMark = fmt.Sprintf("%s %s %s", style.BlockDo, beginContent, style.BlockDone)
doneMark = fmt.Sprintf("%s %s %s", style.BlockDo, endContent, style.BlockDone)
} else {
// For files without comments, return empty markers
return "", ""
}

Expand Down