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

feat: Publish ALL Posts at Once #2 #4

Merged
merged 2 commits into from
Nov 1, 2023
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ The action will push to Medium when your Git commit message contains the "PUBLIS

- Single post: `PUBLISH: file-name.md`
- Multiple posts: `PUBLISH: file1.md, file2.md, ... fileN.md`
- Publish All posts: `PUBLISH: .` or `PUBLISH: all`

## Inputs

- **markdownOrHugo** (required)
- Specify whether the content is in Markdown or Hugo Markdown format.
- Default: "markdown"
- **shortcodes**

- JSON config file location for shortcodes. The config should contain an array of objects, where each object defines a shortcode and its replacement. Config file should be present at your root directory of your project.
- Default: config.json
- Example shortcode config JSON:

```json
[
{
Expand All @@ -31,6 +35,9 @@ The action will push to Medium when your Git commit message contains the "PUBLIS
}
]
```

You can also check my projects shortcode [config file](https://github.com/imrushi/imrushi.github.io/blob/main/shortcodes.json)

- **replaceHyperlinkToLink**
- Replace hyperlinks with links for Medium cards.
- Default: false
Expand All @@ -42,6 +49,7 @@ The action will push to Medium when your Git commit message contains the "PUBLIS
- Default: false

## Environment Variables

The action uses the following environment variables:

- **POST_DIR**:
Expand Down
42 changes: 33 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,37 @@ func extractPostName(commitMsg string) ([]string, []string) {
return c == ','
}
postNames := strings.SplitAfter(commitMsg, "PUBLISH:")[1]

if strings.TrimSpace(postNames) == "." || strings.TrimSpace(postNames) == "all" {
// Extract/Get Content
walkFunc := func(path string, d fs.DirEntry, err error) error {
if err != nil {
log.Fatalf("Error walking directory: %v", err)
return err
}
if strings.ToLower(filepath.Ext(path)) == ".md" {
postNameWithExtSlice = append(postNameWithExtSlice, filepath.Base(path))
}

return nil
}

// Start walking the directory and its subdirectories
err := filepath.WalkDir(postPath, walkFunc)
if err != nil {
log.Fatalf("Error during directory traversal: %v", err)
}

for _, val := range postNameWithExtSlice {
postNameWithExt := strings.TrimSpace(val)
postNameWithDash := strings.Split(postNameWithExt, ".")[0]
c := cases.Title(language.Und)
postName := c.String(strings.Join(strings.Split(postNameWithDash, "-"), " "))
postNameSlice = append(postNameSlice, postName)
}
return postNameWithExtSlice, postNameSlice
}

sliceOfPostName := strings.FieldsFunc(postNames, f)
for _, val := range sliceOfPostName {
postNameWithExt := strings.TrimSpace(val)
Expand Down Expand Up @@ -327,7 +358,7 @@ func main() {
if strings.Contains(commitMsg, "PUBLISH") {
// Extract Post Name from Commit
postNameWithExt, postName := extractPostName(commitMsg)
// fmt.Println("post name: ", postName)

for i := 0; i < len(postName); i++ {
// Extract/Get Content
walkFunc := func(path string, d fs.DirEntry, err error) error {
Expand All @@ -336,7 +367,6 @@ func main() {
return err
}

// fmt.Println(path)
if strings.Contains(path, postNameWithExt[i]) {
content, err := os.ReadFile(path)
if err != nil {
Expand Down Expand Up @@ -386,7 +416,6 @@ func main() {
return err
}

// fmt.Println(path)
if strings.Contains(path, postNameWithExt[i]) {
// 2. Read markdown file
content, err := os.ReadFile(path)
Expand Down Expand Up @@ -418,15 +447,10 @@ func main() {
}
// 5. call API to post on Medium
postRespCode = postToMedium(marshalData)
// fmt.Println(string(data))

return nil
}

// Check if it's a directory
// if d.IsDir() {
// fmt.Println(" (directory)")
// }

return nil
}

Expand Down