Skip to content

Commit

Permalink
[admin] Improve YT description output
Browse files Browse the repository at this point in the history
  • Loading branch information
jerodsanto committed Jan 23, 2025
1 parent 43c4027 commit 4b67960
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[
import_deps: [:ecto, :phoenix],
inputs: ["*.{heex,ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{heex,ex,exs}"],
inputs: ["*.{eex,heex,ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{eex,heex,ex,exs}"],
subdirectories: ["priv/*/migrations"]
]
10 changes: 7 additions & 3 deletions assets/admin/views/episodeView.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export default class EpisodeView {

youtube() {
let $csvFileDropZone = $(".js-csv-file");
let $selectInput = $(".js-episode-select input");
let outputTextArea = document.querySelector(".js-description-output");

$csvFileDropZone
Expand Down Expand Up @@ -121,11 +122,14 @@ export default class EpisodeView {
.querySelector("meta[name='csrf-token']")
.getAttribute("content")
},
body: JSON.stringify({ csv: e.target.result })
body: JSON.stringify({
id: $selectInput.val(),
csv: e.target.result
})
});
let output = await response.json();
let json = await response.json();

outputTextArea.value = output.markers;
outputTextArea.value = json.output;
$csvFileDropZone.removeClass("loading");

outputTextArea.dispatchEvent(new Event("autosize:update"));
Expand Down
9 changes: 9 additions & 0 deletions lib/changelog/kits/string_kit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ defmodule Changelog.StringKit do
Regex.replace(regex, string, "\\1")
end

@doc """
Removes Markdown-style descriptions from a link. Opposite of `md_delinkify`
"""
def md_bare_linkify(string) do
regex = ~r/\[(.*?)\]\((.*?)\)/

Regex.replace(regex, string, "\\2")
end

@doc """
Converts 'bare' mentions to Markdown-style links for further processing
"""
Expand Down
20 changes: 17 additions & 3 deletions lib/changelog_web/controllers/admin/episode_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,22 @@ defmodule ChangelogWeb.Admin.EpisodeController do
|> assign(:news_episodes, news_episodes)
end

def youtube(conn = %{method: "POST"}, %{"csv" => csv}, _podcast) do
data = Changelog.Kits.MarkerKit.to_youtube(csv)
json(conn, %{markers: data})
def youtube(conn = %{method: "POST"}, %{"csv" => csv, "id" => id}, podcast) do
episode = assoc(podcast, :episodes) |> Episode.preload_sponsors() |> Repo.get_by(id: id)
chapters = Changelog.Kits.MarkerKit.to_youtube(csv)

text =
if episode do
Phoenix.View.render_to_string(
ChangelogWeb.Admin.EpisodeView,
"_yt_#{podcast.slug}.text",
%{episode: episode, chapters: chapters}
)
else
chapters
end

json(conn, %{output: String.trim(text)})
end

def youtube(conn, _params, podcast) do
Expand All @@ -114,6 +127,7 @@ defmodule ChangelogWeb.Admin.EpisodeController do
|> assoc(:episodes)
|> Episode.newest_first()
|> Episode.limit(50)
|> Repo.all()

conn
|> assign(:episodes, episodes)
Expand Down
25 changes: 25 additions & 0 deletions lib/changelog_web/templates/admin/episode/_yt_friends.text.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%= @episode.summary %>

SUBSCRIBE:

RSS: https://changelog.am/rss
Apple: https://changelog.am/apple
Spotify: https://changelog.am/spotify
Android: https://changelog.am/android
Overcast: https://changelog.am/overcast
Email: https://changelog.am/email
X: https://x.com/changelog
Bluesky: https://bsky.app/profile/changelog.com
Mastodon: https://changelog.social/@changelog

SPONSORS:
<%= for sponsor <- @episode.episode_sponsors do %>
<%= sponsor.title %> - <%= sponsor.description |> SharedHelpers.md_to_text() %> <%= sponsor.link_url %>
<% end %>
LINKS:

<%= @episode.notes |> Changelog.StringKit.md_bare_linkify() %>

CHAPTERS:

<%= @chapters %>
7 changes: 7 additions & 0 deletions lib/changelog_web/templates/admin/episode/_yt_news.text.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Moar links worth your attention! 👉 <%= EpisodeView.share_url(@episode) %>

<%= SharedHelpers.md_to_text(@episode.summary) %>

CHAPTERS:
<%= for chapter <- @episode.audio_chapters do %>
<%= TimeView.duration(chapter.starts_at) %> <%= chapter.title %><% end %>
25 changes: 25 additions & 0 deletions lib/changelog_web/templates/admin/episode/_yt_podcast.text.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%= SharedHelpers.md_to_text(@episode.summary) %>

SUBSCRIBE:

RSS: https://changelog.fm/rss
Apple: https://changelog.fm/apple
Spotify: https://changelog.fm/spotify
Android: https://changelog.fm/android
Overcast: https://changelog.fm/overcast
Email: https://changelog.fm/email
X: https://x.com/changelog
Bluesky: https://bsky.app/profile/changelog.com
Mastodon: https://changelog.social/@changelog

SPONSORS:
<%= for sponsor <- @episode.episode_sponsors do %>
<%= sponsor.title %> - <%= sponsor.description |> SharedHelpers.md_to_text() %> <%= sponsor.link_url %>
<% end %>
LINKS:

<%= @episode.notes |> Changelog.StringKit.md_bare_linkify() %>

CHAPTERS:

<%= @chapters %>
24 changes: 19 additions & 5 deletions lib/changelog_web/templates/admin/episode/youtube.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,36 @@

<div class="ui basic segment text container">
<form class="ui form">
<div class="field">
<div class="ui selection dropdown js-episode-select">
<input type="hidden" name="episode">
<i class="dropdown icon"></i>
<div class="default text">Select an episode</div>
<div class="menu">
<%= for episode <- @episodes do %>
<div class="item" data-value={episode.id}><%= numbered_title(episode) %></div>
<% end %>

</div>
</div>
</div>

<div class="ui padded segment js-csv-file">
<div class="ui icon message">
<i class="file audio icon"></i>
<div class="content">
<div class="header">
Drop a markered .CSV file here to get a YouTube description
Select an episode & drop a markered .CSV file here
</div>
</div>
</div>
</div>

<div class="ui padded segment">
<div class="field">
<label>Output</label>
<textarea class="js-description-output" rows="1"></textarea>
<div class="field">
<label>Description</label>
<textarea class="js-description-output" rows="1"></textarea>
</div>
</div>
</div>
</form>
</div>
21 changes: 21 additions & 0 deletions test/changelog/kits/string_kit_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,27 @@ defmodule Changelog.StringKitTest do
end
end

describe "md_bare_linkify/1" do
test "it leaves strings with no links in them alone" do
raw = "this has zero ! links."
assert StringKit.md_bare_linkify(raw) == raw
end

test "it works with a single link in there" do
a = "This has [one](http://test.com) link and that's it"
b = "This has http://test.com link and that's it"

assert StringKit.md_bare_linkify(a) == b
end

test "it works with multiple links in there" do
a = "This has [two](http://test.com) links and that's [all](http://www.com)."
b = "This has http://test.com links and that's http://www.com."

assert StringKit.md_bare_linkify(a) == b
end
end

describe "mentions_linkify/1" do
test "it no-ops when no mentions to linkify" do
raw = ~s{Yo here is my _super cool_ thing}
Expand Down

0 comments on commit 4b67960

Please sign in to comment.