Skip to content

Commit

Permalink
Unify navigation for easier updating (fix bug) (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
natolambert authored Jan 3, 2025
1 parent ff742b9 commit 012c844
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 159 deletions.
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ HTML_ARGS = --template templates/html.html --standalone --to html5
PDF_ARGS = --template templates/pdf.tex --pdf-engine xelatex
NESTED_HTML_TEMPLATE = templates/chapter.html

# Add this with your other file variables at the top
JS_FILES = $(shell find . -name '*.js') # This will find all .js files in the current directory and subdirectories

# Per-format file dependencies

BASE_DEPENDENCIES = $(MAKEFILE) $(CHAPTERS) $(METADATA) $(IMAGES) $(TEMPLATES)
Expand Down Expand Up @@ -86,6 +89,7 @@ clean:
# Debugging output for chapters and HTML output paths
$(info Chapters found: $(CHAPTERS))
$(info HTML output will be: $(CHAPTER_HTMLS))
$(info JS files found: $(JS_FILES))

####################################################################################################
# File builders
Expand Down Expand Up @@ -120,6 +124,8 @@ $(BUILD)/html/$(OUTPUT_FILENAME_HTML).html: $(HTML_DEPENDENCIES)
$(MKDIR_CMD) $(BUILD)/html
$(CONTENT) | $(CONTENT_FILTERS) | $(PANDOC_COMMAND) $(ARGS) $(HTML_ARGS) -o $@
$(COPY_CMD) $(IMAGES) $(BUILD)/html/ --mathjax
$(COPY_CMD) $(JS_FILES) $(BUILD)/html/
$(COPY_CMD) $(JS_FILES) $(BUILD)/html/c/ # Copy to nested directory
$(ECHO_BUILT)

# Nested HTML build targets
Expand Down Expand Up @@ -158,4 +164,6 @@ files:
cp favicon.ico $(BUILD)/html/ || echo "Failed to copy to $(BUILD)/html/"
cp favicon.ico $(BUILD)/html/c/ || echo "Failed to copy to $(BUILD)/html/c/"
cp $(BUILD)/pdf/book.pdf $(BUILD)/html/ || echo "Failed to copy to $(BUILD)/html/"
cp -r images $(BUILD)/html/c/ || echo "Failed to copy to $(BUILD)/html/chapters/"
cp -r images $(BUILD)/html/c/ || echo "Failed to copy to $(BUILD)/html/chapters/"
cp ./templates/nav.js $(BUILD)/html/ || echo "Failed to copy nav.js to $(BUILD)/html/"
cp ./templates/nav.js $(BUILD)/html/c/ || echo "Failed to copy nav.js to $(BUILD)/html/c/"
102 changes: 8 additions & 94 deletions templates/chapter.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
$for(header-includes)$
$header-includes$
$endfor$

<!-- custom js nav -->
<script src="nav.js" defer></script>

</head>
<body>
$for(include-before)$
Expand All @@ -52,102 +56,12 @@ <h1 class="title"><a href="www.rlhfbook.com" style="color: inherit; text-decorat
<p class="subtitle">$subtitle$</p>
$endif$

<style>
.inactive {
color: #666;
}

@media (min-width: 768px) {
.chapter-nav {
grid-template-columns: repeat(3, 1fr);
}
}
</style>

<div>
<button class="dropdown-button" aria-expanded="false">
<span><strong>Navigation</strong></span>
<svg class="chevron" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M19 9l-7 7-7-7" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</button>

<div class="dropdown-content">
<nav class="chapter-nav">
<div class="section">
<h3>Links</h3>
<ol>
<li><a href="https://rlhfbook.com">Home</a></li>
<li><a href="https://github.com/natolambert/rlhf-book">GitHub Repository</a></li>
<li><a href="https://rlhfbook.com/book.pdf">PDF</a></li>
<li class="inactive">Order a copy (Soon)</li>
</ol>
</div>

<div class="section">
<h3>Introductions</h3>
<ol>
<li><a href="https://rlhfbook.com/c/01-introduction.html">Introduction</a></li>
<li><a href="https://rlhfbook.com/c/02-preferences.html">What are preferences?</a></li>
<li><a href="https://rlhfbook.com/c/03-optimization.html">Optimization and RL</a></li>
<li><a href="https://rlhfbook.com/c/04-related-works.html">Seminal (Recent) Works</a></li>
</ol>
</div>

<div class="section">
<h3>Problem Setup</h3>
<ol>
<li><a href="https://rlhfbook.com/c/05-setup.html">Definitions</a></li>
<li><a href="https://rlhfbook.com/c/06-preference-data.html">Preference Data</a></li>
<li><a href="https://rlhfbook.com/c/07-reward-models.html">Reward Modeling</a></li>
<li><a href="https://rlhfbook.com/c/08-regularization.html">Regularization</a></li>
</ol>
</div>

<div class="section">
<h3>Optimization</h3>
<ol>
<li><a href="https://rlhfbook.com/c/09-instruction-tuning.html">Instruction Tuning</a></li>
<li><a href="https://rlhfbook.com/c/10-rejection-sampling.html">Rejection Sampling</a></li>
<li><a href="https://rlhfbook.com/c/11-policy-gradients.html">Policy Gradients</a></li>
<li><a href="https://rlhfbook.com/c/12-direct-alignment.html">Direct Alignment Algorithms</a></li>
</ol>
</div>

<div class="section">
<h3>Advanced (TBD)</h3>
<ol>
<li><a href="https://rlhfbook.com/c/13-cai.html">Constitutional AI and AI Feedback</a></li>
<li><a href="https://rlhfbook.com/c/14-reasoning.html">Reasoning and Reinforcement Finetuning</a></li>
<li><a href="https://rlhfbook.com/c/15-synthetic.html">Synthetic Data</a></li>
<li><a href="https://rlhfbook.com/c/16-evaluation.html">Evaluation</a></li>
</ol>
</div>
$for(author)$
<p class="author">$author$</p>
$endfor$

<div class="section">
<h3>Open Questions (TBD)</h3>
<ol>
<li><a href="https://rlhfbook.com/c/17-over-optimization.html">Over-optimization</a></li>
<li class="inactive">Style</li>
</ol>
</div>
</nav>
</div>
</div>
<navigation-dropdown expanded="false"></navigation-dropdown>

<script>
document.addEventListener('DOMContentLoaded', function() {
const button = document.querySelector('.dropdown-button');
const content = document.querySelector('.dropdown-content');

button.addEventListener('click', function() {
const isExpanded = button.getAttribute('aria-expanded') === 'true';
button.setAttribute('aria-expanded', !isExpanded);
content.classList.toggle('open');
});
});
</script>

</header>
$endif$
<div> <h3> Chapter Contents </h3> </div>
Expand Down
70 changes: 6 additions & 64 deletions templates/html.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
$for(header-includes)$
$header-includes$
$endfor$

<!-- custom js nav -->
<script src="nav.js" defer></script>

</head>
<body>
$for(include-before)$
Expand All @@ -51,73 +55,11 @@ <h1 class="title">$title$</h1>
$if(subtitle)$
<p class="subtitle">$subtitle$</p>
$endif$

<nav class="chapter-nav">
<div class="section">
<h3>Links</h3>
<ol>
<li><a href="https://rlhfbook.com">Home</a></li>
<li><a href="https://github.com/natolambert/rlhf-book">GitHub Repository</a></li>
<li><a href="https://rlhfbook.com/book.pdf">PDF</a></li>
<li class="inactive">Order a copy (Soon)</li>
</ol>
</div>

<div class="section">
<p><strong>Introductions</strong></p>
<ol>
<li><a href="https://rlhfbook.com/c/01-introduction.html">Introduction</a></li>
<li><a href="https://rlhfbook.com/c/02-preferences.html">What are preferences?</a></li>
<li><a href="https://rlhfbook.com/c/03-optimization.html">Optimization and RL</a></li>
<li><a href="https://rlhfbook.com/c/04-related-works.html">Seminal (Recent) Works</a></li>
</ol>
</div>

<div class="section">
<p><strong>Problem Setup</strong></p>
<ol>
<li><a href="https://rlhfbook.com/c/05-setup.html">Definitions</a></li>
<li><a href="https://rlhfbook.com/c/06-preference-data.html">Preference Data</a></li>
<li><a href="https://rlhfbook.com/c/07-reward-models.html">Reward Modeling</a></li>
<li><a href="https://rlhfbook.com/c/08-regularization.html">Regularization</a></li>
</ol>
</div>

<div class="section">
<p><strong>Optimization</strong></p>
<ol>
<li><a href="https://rlhfbook.com/c/09-instruction-tuning.html">Instruction Tuning</a></li>
<li><a href="https://rlhfbook.com/c/10-rejection-sampling.html">Rejection Sampling</a></li>
<li><a href="https://rlhfbook.com/c/11-policy-gradients.html">Policy Gradients</a></li>
<li><a href="https://rlhfbook.com/c/12-direct-alignment.html">Direct Alignment Algorithms</a></li>
</ol>
</div>

<div class="section">
<p><strong>Advanced (TBD)</strong></p>
<ol>
<li><a href="https://rlhfbook.com/c/13-cai.html">Constitutional AI and AI Feedback</a></li>
<li><a href="https://rlhfbook.com/c/14-reasoning.html">Reasoning and Reinforcement Finetuning</a></li>
<li><a href="https://rlhfbook.com/c/15-synthetic.html">Synthetic Data</a></li>
<li><a href="https://rlhfbook.com/c/16-evaluation.html">Evaluation</a></li>
</ol>
</div>

<div class="section">
<p><strong>Open Questions (TBD)</strong></p>
<ol>
<li><a href="https://rlhfbook.com/c/17-over-optimization.html">Over-optimization</a></li>
<li>Style</li>
</ol>
</div>
</nav>

$for(author)$
<p class="author">$author$</p>
$endfor$
$if(date)$
<p class="date">$date$</p>
$endif$
<navigation-dropdown expanded="true"></navigation-dropdown>

$if(abstract)$
<div class="abstract">
<h2>Abstract</h2>
Expand Down
114 changes: 114 additions & 0 deletions templates/nav.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
class NavigationDropdown extends HTMLElement {
constructor() {
super();

// Get the initial expanded state from the attribute, default to false
const initialExpanded = this.getAttribute('expanded') === 'true';

this.innerHTML = `
<div>
<button class="dropdown-button" aria-expanded="${initialExpanded}">
<span><strong>Navigation</strong></span>
<svg class="chevron" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M19 9l-7 7-7-7" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</button>
<div class="dropdown-content${initialExpanded ? ' open' : ''}">
<nav class="chapter-nav">
<div class="section">
<h3>Links</h3>
<ol>
<li><a href="https://rlhfbook.com">Home</a></li>
<li><a href="https://github.com/natolambert/rlhf-book">GitHub Repository</a></li>
<li><a href="https://rlhfbook.com/book.pdf">PDF</a></li>
<li class="inactive">Order a copy (Soon)</li>
</ol>
</div>
<div class="section">
<h3>Introductions</h3>
<ol>
<li><a href="https://rlhfbook.com/c/01-introduction.html">Introduction</a></li>
<li><a href="https://rlhfbook.com/c/02-preferences.html">What are preferences?</a></li>
<li><a href="https://rlhfbook.com/c/03-optimization.html">Optimization and RL</a></li>
<li><a href="https://rlhfbook.com/c/04-related-works.html">Seminal (Recent) Works</a></li>
</ol>
</div>
<div class="section">
<h3>Problem Setup</h3>
<ol>
<li><a href="https://rlhfbook.com/c/05-setup.html">Definitions</a></li>
<li><a href="https://rlhfbook.com/c/06-preference-data.html">Preference Data</a></li>
<li><a href="https://rlhfbook.com/c/07-reward-models.html">Reward Modeling</a></li>
<li><a href="https://rlhfbook.com/c/08-regularization.html">Regularization</a></li>
</ol>
</div>
<div class="section">
<h3>Optimization</h3>
<ol>
<li><a href="https://rlhfbook.com/c/09-instruction-tuning.html">Instruction Tuning</a></li>
<li><a href="https://rlhfbook.com/c/10-rejection-sampling.html">Rejection Sampling</a></li>
<li><a href="https://rlhfbook.com/c/11-policy-gradients.html">Policy Gradients</a></li>
<li><a href="https://rlhfbook.com/c/12-direct-alignment.html">Direct Alignment Algorithms</a></li>
</ol>
</div>
<div class="section">
<h3>Advanced (TBD)</h3>
<ol>
<li><a href="https://rlhfbook.com/c/13-cai.html">Constitutional AI and AI Feedback</a></li>
<li><a href="https://rlhfbook.com/c/14-reasoning.html">Reasoning and Reinforcement Finetuning</a></li>
<li><a href="https://rlhfbook.com/c/15-synthetic.html">Synthetic Data</a></li>
<li><a href="https://rlhfbook.com/c/16-evaluation.html">Evaluation</a></li>
</ol>
</div>
<div class="section">
<h3>Open Questions (TBD)</h3>
<ol>
<li><a href="https://rlhfbook.com/c/17-over-optimization.html">Over-optimization</a></li>
<li class="inactive">Style</li>
</ol>
</div>
</nav>
</div>
</div>
`;

// Set up click handler
const button = this.querySelector('.dropdown-button');
const content = this.querySelector('.dropdown-content');

button.addEventListener('click', () => {
const isExpanded = button.getAttribute('aria-expanded') === 'true';
button.setAttribute('aria-expanded', !isExpanded);
content.classList.toggle('open');
});
}

// Add attribute change observer
static get observedAttributes() {
return ['expanded'];
}

attributeChangedCallback(name, oldValue, newValue) {
if (name === 'expanded') {
const button = this.querySelector('.dropdown-button');
const content = this.querySelector('.dropdown-content');
const isExpanded = newValue === 'true';

if (button && content) {
button.setAttribute('aria-expanded', isExpanded);
content.classList.toggle('open', isExpanded);
}
}
}
}

// Only define the component once
if (!customElements.get('navigation-dropdown')) {
customElements.define('navigation-dropdown', NavigationDropdown);
}
7 changes: 7 additions & 0 deletions templates/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,13 @@ table td {
}
}

.dropdown-content {
display: none;
}

.dropdown-content.open {
display: block;
}
/* Header Nav Block */
.chapter-nav {
display: grid;
Expand Down

0 comments on commit 012c844

Please sign in to comment.