11# standard lib
22import logging
33import re
4+ import os
45
56# 3rd party
67from mkdocs .config import config_options
78from mkdocs .plugins import BasePlugin
89from mkdocs .structure .nav import Page
10+ from mkdocs .utils import copy_file
911
1012# package modules
1113from mkdocs_git_revision_date_localized_plugin .util import Util
1214
15+ HERE = os .path .dirname (os .path .abspath (__file__ ))
1316
1417class 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 )
0 commit comments