Skip to content

Commit 6d61521

Browse files
committed
Add timeago js and css files locally, see #39 and #42
1 parent c53dea5 commit 6d61521

File tree

9 files changed

+68
-62
lines changed

9 files changed

+68
-62
lines changed

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include mkdocs_git_revision_date_localized_plugin/js/*.js
2+
include mkdocs_git_revision_date_localized_plugin/css/*.css
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
timeago output is dynamic, which breaks when you print a page
3+
This ensures fallback to type "iso_date" when printing
4+
*/
5+
6+
.git-revision-date-localized-plugin-iso_date { display: none }
7+
8+
@media print {
9+
.git-revision-date-localized-plugin-iso_date { display: inline }
10+
.git-revision-date-localized-plugin-timeago { display: none }
11+
}

mkdocs_git_revision_date_localized_plugin/js/timeago.locales.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mkdocs_git_revision_date_localized_plugin/js/timeago.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Script to ensure timeago keeps working when
2+
// used with mkdocs-material's instant loading feature
3+
4+
if (
5+
typeof app !== "undefined" &&
6+
typeof app.document$ !== "undefined"
7+
) {
8+
app.document$.subscribe(function() {
9+
var nodes = document.querySelectorAll('.timeago');
10+
var locale = nodes[0].getAttribute('locale');
11+
timeago().render(nodes, locale);
12+
})
13+
} else {
14+
var nodes = document.querySelectorAll('.timeago');
15+
var locale = nodes[0].getAttribute('locale');
16+
timeago().render(nodes, locale);
17+
}

mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 22 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# standard lib
22
import logging
33
import re
4+
import os
45

56
# 3rd party
67
from mkdocs.config import config_options
78
from mkdocs.plugins import BasePlugin
89
from mkdocs.structure.nav import Page
10+
from mkdocs.utils import copy_file
911

1012
# package modules
1113
from mkdocs_git_revision_date_localized_plugin.util import Util
1214

15+
HERE = os.path.dirname(os.path.abspath(__file__))
1316

1417
class GitRevisionDateLocalizedPlugin(BasePlugin):
1518
config_scheme = (
@@ -87,6 +90,13 @@ def on_config(self, config: config_options.Config, **kwargs) -> dict:
8790
# set locale also in plugin configuration
8891
self.config["locale"] = locale_set
8992

93+
# Add pointers to support files for timeago.js
94+
if self.config.get("type") == "timeago":
95+
config["extra_javascript"] = ["js/timeago_mkdocs_material.js"] + config["extra_javascript"]
96+
config["extra_javascript"] = ["js/timeago.locales.min.js"] + config["extra_javascript"]
97+
config["extra_javascript"] = ["js/timeago.min.js"] + config["extra_javascript"]
98+
config["extra_css"] = ["css/timeago.css"] + config["extra_css"]
99+
90100
return config
91101

92102
def on_page_markdown(
@@ -122,7 +132,7 @@ def on_page_markdown(
122132

123133
# timeago output is dynamic, which breaks when you print a page
124134
# This ensures fallback to type "iso_date"
125-
# controlled via CSS (see on_post_page() event)
135+
# controlled via CSS (see on_post_build() event)
126136
if self.config["type"] == "timeago":
127137
revision_date += revision_dates["iso_date"]
128138

@@ -134,65 +144,16 @@ def on_page_markdown(
134144
flags=re.IGNORECASE,
135145
)
136146

137-
def on_post_page(self, output_content: str, **kwargs) -> str:
138-
"""
139-
Add timeago.js as a CDN to the HTML page.
140-
The CDN with latest version timeago.js can be found on
141-
https://cdnjs.com/libraries/timeago.js
142-
143-
The `post_template` event is called after the template is rendered,
144-
but before it is written to disc and can be used to alter the output
145-
of the page. If an empty string is returned, the page is skipped
146-
and nothing is written to disc.
147-
148-
https://www.mkdocs.org/user-guide/plugins/#on_post_page
149-
150-
Args:
151-
output_content (str): output of rendered template as string
152-
153-
Returns:
154-
str: output of rendered template as string
155-
"""
156-
157-
if self.config.get("type") != "timeago":
158-
return output_content
159-
160-
# Insert timeago.js dependencies
161-
extra_js = """
162-
<script src="https://cdnjs.cloudflare.com/ajax/libs/timeago.js/4.0.0-beta.2/timeago.min.js"></script>
163-
<script src="https://cdnjs.cloudflare.com/ajax/libs/timeago.js/4.0.0-beta.2/timeago.locales.min.js"></script>
164-
<script>
165-
if (
166-
typeof app !== "undefined" &&
167-
typeof app.document$ !== "undefined"
168-
) {
169-
app.document$.subscribe(function() {
170-
var nodes = document.querySelectorAll('.timeago');
171-
var locale = nodes[0].getAttribute('locale');
172-
timeago.render(nodes, locale);
173-
})
174-
} else {
175-
var nodes = document.querySelectorAll('.timeago');
176-
var locale = nodes[0].getAttribute('locale');
177-
timeago.render(nodes, locale);
178-
}
179-
</script>
180-
"""
181-
idx = output_content.index("</body>")
182-
output_content = output_content[:idx] + extra_js + output_content[idx:]
183147

184-
# timeago output is dynamic, which breaks when you print a page
185-
# This ensures fallback to type "iso_date"
186-
extra_css = """
187-
<style>
188-
.git-revision-date-localized-plugin-iso_date { display: none }
189-
@media print {
190-
.git-revision-date-localized-plugin-iso_date { display: inline }
191-
.git-revision-date-localized-plugin-timeago { display: none }
192-
}
193-
</style>
194-
"""
195-
idx = output_content.index("</head>")
196-
output_content = output_content[:idx] + extra_css + output_content[idx:]
148+
def on_post_build(self, config, **kwargs):
197149

198-
return output_content
150+
# Add timeago files:
151+
# Current version timeago.js: 2.0.2
152+
if self.config.get("type") == "timeago":
153+
files = ['js/timeago.min.js', 'js/timeago.locales.min.js',
154+
'js/timeago_mkdocs_material.js', 'css/timeago.css']
155+
for file in files:
156+
dest_file_path = os.path.join(config["site_dir"], file)
157+
src_file_path = os.path.join(HERE, file)
158+
assert os.path.exists(src_file_path)
159+
copy_file(src_file_path, dest_file_path)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
url="https://github.com/timvink/mkdocs-git-revision-date-localized-plugin",
1414
author="Tim Vink",
1515
author_email="[email protected]",
16+
include_package_data=True,
1617
license="MIT",
1718
python_requires=">=3.5",
1819
classifiers=[
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# subpage
2+
3+
nested.

tests/fixtures/basic_project/mkdocs_theme_timeago.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,13 @@ theme:
77
plugins:
88
- search
99
- git-revision-date-localized:
10-
type: timeago
10+
type: timeago
11+
12+
13+
nav:
14+
- index.md
15+
- first_page.md
16+
- Subpage:
17+
- subpage.md
18+
- second_page.md
19+
- page_with_tag.md

0 commit comments

Comments
 (0)