-
Notifications
You must be signed in to change notification settings - Fork 39
Add code-preview layout and web component example page #685
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
Changes from 1 commit
921e5f6
46204c6
0d92963
747e8f5
3eda802
b831aa2
97d21d6
9109365
5b54856
fb0f66f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,5 +47,6 @@ exclude: | |
| - vendor/gems/ | ||
| - vendor/ruby/ | ||
| - STYLE_GUIDE.md | ||
| - "**/README.md" | ||
|
|
||
| include: ["CNAME"] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| <!-- Displays another page inside an iframe with source code on the left and the page on the right | ||
|
|
||
| For maintainers: Set the url to display using the iframe_content_url variable in the frontmatter. | ||
| --> | ||
|
|
||
| <!doctype html> | ||
|
|
||
| <html lang="en"> | ||
| <head> | ||
| <title>{{ page.title }} - Docs - Data Commons</title> | ||
juliawu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | ||
| <meta | ||
| name="viewport" | ||
| content="width=device-width, initial-scale=1, shrink-to-fit=no" | ||
| /> | ||
| <link rel="icon" href="/assets/images/favicon.png" type="image/png" /> | ||
| <link | ||
| href="https://fonts.googleapis.com/css2?family=Google+Sans:wght@300;400;500;700" | ||
| rel="stylesheet" | ||
| /> | ||
| <link | ||
| href="https://fonts.googleapis.com/css2?family=Google+Sans+Text:wght@300;400;500;700" | ||
| rel="stylesheet" | ||
| /> | ||
| <link | ||
| href="https://fonts.googleapis.com/css2?family=Public+Sans:wght@600&family=Roboto&family=Material+Icons&display=swap" | ||
| rel="stylesheet" | ||
| /> | ||
| <link href="/assets/css/styles.css" rel="stylesheet" /> | ||
| <link href="/assets/css/code-preview.css" rel="stylesheet" /> | ||
| {% if page.url == "/api/web_components/" or page.parent == "Web Components" | ||
juliawu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| %} | ||
| <script src="https://datacommons.org/datacommons.js"></script> | ||
| {% endif %} | ||
| <!-- Syntax highlighting for code --> | ||
| <link | ||
| rel="stylesheet" | ||
| href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.min.css" | ||
| /> | ||
| <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script> | ||
| </head> | ||
| <body> | ||
| <div id="main"> | ||
| {% include header.html %} | ||
| <main id="{{ main_id }}" class="container-fluid"> | ||
| <div id="content"> | ||
| <div id="source-code"> | ||
| <h3>Source Code</h3> | ||
| <pre><code id="code-target" class="language-html">Loading source code...</code></pre> | ||
| </div> | ||
| <div id="preview"> | ||
| <h3>Preview</h3> | ||
juliawu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <iframe id="iframe" src="{{ page.iframe_content_url }}"></iframe> | ||
|
||
| </div> | ||
| </div> | ||
| </main> | ||
| {% include footer.html %} | ||
| </div> | ||
| <script> | ||
| // Fetch the source code for the page and display it in the source code | ||
| // div. | ||
| const fileUrl = "{{ page.iframe_content_url }}"; | ||
juliawu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fetch(fileUrl) | ||
| .then((response) => { | ||
| if (!response.ok) throw new Error("Network response was not ok"); | ||
| return response.text(); | ||
| }) | ||
| .then((data) => { | ||
| // Escape the HTML so it doesn't render as actual elements | ||
| const codeTarget = document.getElementById("code-target"); | ||
| codeTarget.textContent = data; | ||
| Prism.highlightElement(codeTarget); | ||
| }) | ||
| .catch((err) => { | ||
| document.getElementById("code-target").textContent = | ||
| "Error loading source: " + err.message; | ||
| }); | ||
| </script> | ||
| {% include scripts.html %} | ||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Web Components Examples | ||
|
|
||
| This directory contains the pages that render the side-by-side source code and rendered output for the Data Commons web components examples. | ||
|
|
||
| ## How the examples are rendered | ||
|
|
||
| The examples are rendered into the `code-preview` layout in `/_layouts/code-preview.html`. | ||
|
|
||
| A `<script>` tag in the layout pulls the the raw HTML source of the file specified in the `iframe_content_url` frontmatter property and displays it in a `<code>` block. It then applies syntax highlighting to the code via [Prism.js](https://prismjs.com/). | ||
|
|
||
| The right-side live-render is an iframe that loads the same file. | ||
|
|
||
| ## How to add a new example | ||
|
|
||
| 1. Add a new HTML file to the assets/examples/web-components/ directory containing the full HTML for the example. | ||
| 2. Create a new markdown file in this directory that uses the code-preview layout. | ||
| 3. Add frontmatter to the new file following this template: | ||
|
|
||
| ```yaml | ||
| --- | ||
| layout: code-preview | ||
| title: Web components example - [SOME DESCRIPTIVE NAME HERE] | ||
| published: true | ||
| nav_exclude: true # This keeps the page from showing up in the left navigation | ||
| iframe_content_url: /assets/examples/web-components/[FILENAME].html | ||
| --- | ||
| ``` | ||
|
|
||
| The `iframe_content_url` should point to the HTML file you created in step 1. | ||
|
|
||
| 4. Finally, you can link to the new example using the path `/api/web_components/examples/[FILENAME].html` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| layout: code-preview | ||
| title: All Charts | ||
| parent: All Charts - Web Components Example | ||
| published: true | ||
| nav_exclude: true | ||
| iframe_content_url: /assets/examples/web-components/all-charts.html | ||
juliawu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| --- | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| --- | ||
| --- | ||
|
|
||
| /** | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| /** | ||
| * CSS for the code-preview layout (e.g., /api/web_components/examples/all-charts.html) | ||
| */ | ||
|
|
||
| html, | ||
| body { | ||
| height: 100vh; | ||
| } | ||
|
|
||
| #main { | ||
| display: flex; | ||
| flex-direction: column; | ||
| height: 100%; | ||
|
|
||
| > main { | ||
| flex-grow: 1; | ||
| display: flex; | ||
| flex-direction: column; | ||
| min-height: 0; | ||
| padding: 0; | ||
| } | ||
| } | ||
|
|
||
| header, | ||
| footer { | ||
| flex-shrink: 0; | ||
| } | ||
|
|
||
| #content { | ||
| flex-grow: 1; | ||
| margin: 0; | ||
| min-height: 0; | ||
| display: flex; | ||
| overflow: hidden; | ||
|
|
||
| /* On smaller screens, show source code and preview top to bottom */ | ||
| @media (max-width: 1024px) { | ||
| flex-direction: column; | ||
| } | ||
| } | ||
|
|
||
| #preview, | ||
| #source-code { | ||
| width: 50%; | ||
| height: 100%; | ||
| display: flex; | ||
| flex-direction: column; | ||
| overflow: hidden; | ||
|
|
||
| h3 { | ||
| margin: 0; | ||
| padding: 10px; | ||
| background: #fafcff; | ||
| border-top: 1px solid #dee2e6; | ||
| border-bottom: 1px solid #dee2e6; | ||
| flex-shrink: 0; | ||
| font-size: 1rem; | ||
| font-weight: 500; | ||
| } | ||
|
|
||
| /* On smaller screens, show source code and preview top to bottom */ | ||
| @media (max-width: 1024px) { | ||
| width: 100%; | ||
| height: 50%; | ||
| min-height: 0; | ||
| } | ||
| } | ||
|
|
||
| #preview { | ||
| iframe { | ||
| border: none; | ||
| width: 100%; | ||
| flex-grow: 1; | ||
| min-height: 0; | ||
| } | ||
| } | ||
|
|
||
| #source-code { | ||
| pre { | ||
| margin: 0; | ||
| flex-grow: 1; | ||
| overflow: auto; | ||
| min-height: 0; | ||
| padding: 10px; | ||
| } | ||
| } | ||
|
|
||
| #code-target { | ||
| /* Reset specific height/overflow as pre handles it now */ | ||
| height: auto; | ||
| overflow: visible; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.