Skip to content

POC: Feature random code snippet on landing page #303

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

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
35 changes: 35 additions & 0 deletions _data/featured_snippets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
- |
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
print("Hello, \(name)!")
}
// Hello, Anna!
// Hello, Alex!
// Hello, Brian!
// Hello, Jack!
- |
func greet(person: String, alreadyGreeted: Bool) -> String {
if alreadyGreeted {
return greetAgain(person: person)
} else {
return greet(person: person)
}
}
print(greet(person: "Tim", alreadyGreeted: true))
// Prints "Hello again, Tim!"
- |
func oneMore(than number: Int) -> Int {
return number + 1
}

var myNumber = 1
myNumber = oneMore(than: myNumber)
print(myNumber)
// Prints "2"
- |
enum Beverage: CaseIterable {
case coffee, tea, juice
}
let numberOfChoices = Beverage.allCases.count
print("\(numberOfChoices) beverages available")
// Prints "3 beverages available"
22 changes: 22 additions & 0 deletions assets/stylesheets/pages/_landing.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@
}
}

.featured-snippet {
display: none;

&.visible {
display: block;
}

&.highlighter-rouge {
margin-left: 0px;
}

pre {
height: 15rem;
border: 1px solid var(--color-fill-tertiary);
border-radius: 4px;
padding: 1.25rem 1.5rem;
margin: 0px;
}
}

.link-grid {
ul {
display: grid;
Expand Down Expand Up @@ -55,9 +75,11 @@
.flex-container {
display: flex;
flex-direction: column;
align-items: center;
height: 100%;

.latest-release-container {
height: 64px;
flex-grow: 1;
display: flex;
align-items: center;
Expand Down
16 changes: 16 additions & 0 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ atom: true

Swift is a general-purpose programming language built using a modern approach to safety, performance, and software design patterns.

{% for snippet in site.data.featured_snippets %}
```swift
{{ snippet -}}
```
{: class="featured-snippet {% if forloop.first %}visible{% endif %}" }
{% endfor %}

<div class="link-grid" markdown="1">
<ul>
<li>
Expand Down Expand Up @@ -126,3 +133,12 @@ Stay up-to-date with the latest in the Swift community.
- [Visit the Swift forums](https://forums.swift.org)
- [Follow @Swiftlang on Twitter](https://twitter.com/swiftlang){:target="_blank" class="link-external"}
</div>

<script>
var featuredSnippets = document.querySelectorAll('.featured-snippet');
var visibleSnippet = document.querySelector('.featured-snippet.visible');
var randomIndex = Math.floor(Math.random() * featuredSnippets.length);

visibleSnippet?.classList.remove('visible');
featuredSnippets[randomIndex]?.classList.add('visible');
</script>