Skip to content

Commit 061522c

Browse files
authored
Merge pull request #5344 from saadmk11/insert_mkdocs_media
improvement on inserting mkdocs media
2 parents bcc3aa8 + b23f36d commit 061522c

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

readthedocs/doc_builder/backends/mkdocs.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,25 @@ def append_conf(self, **__):
140140
),
141141
)
142142

143-
user_config.setdefault('extra_javascript', []).extend([
143+
extra_javascript_list = [
144144
'readthedocs-data.js',
145145
'%score/js/readthedocs-doc-embed.js' % static_url,
146146
'%sjavascript/readthedocs-analytics.js' % static_url,
147-
])
148-
user_config.setdefault('extra_css', []).extend([
147+
]
148+
extra_css_list = [
149149
'%scss/badge_only.css' % static_url,
150150
'%scss/readthedocs-doc-embed.css' % static_url,
151-
])
151+
]
152+
153+
# Only add static file if the files are not already in the list
154+
user_config.setdefault('extra_javascript', []).extend(
155+
[js for js in extra_javascript_list if js not in user_config.get(
156+
'extra_javascript')]
157+
)
158+
user_config.setdefault('extra_css', []).extend(
159+
[css for css in extra_css_list if css not in user_config.get(
160+
'extra_css')]
161+
)
152162

153163
# The docs path is relative to the location
154164
# of the mkdocs configuration file.

readthedocs/rtd_tests/tests/test_doc_builder.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ def test_get_theme_name_with_feature_flag(self, checkout_path, run):
279279
mock.ANY,
280280
)
281281

282-
283282
@patch('readthedocs.doc_builder.base.BaseBuilder.run')
284283
@patch('readthedocs.projects.models.Project.checkout_path')
285284
def test_append_conf_create_yaml(self, checkout_path, run):
@@ -420,7 +419,6 @@ def test_append_conf_existing_yaml_on_root_with_invalid_setting(self, checkout_p
420419
with self.assertRaises(MkDocsYAMLParseError):
421420
self.searchbuilder.append_conf()
422421

423-
424422
@patch('readthedocs.doc_builder.base.BaseBuilder.run')
425423
@patch('readthedocs.projects.models.Project.checkout_path')
426424
def test_dont_override_theme(self, checkout_path, run):
@@ -489,3 +487,54 @@ def test_write_js_data_docs_dir(self, checkout_path, run, generate_rtd_data):
489487
docs_dir='docs',
490488
mkdocs_config=mock.ANY,
491489
)
490+
491+
@patch('readthedocs.doc_builder.base.BaseBuilder.run')
492+
@patch('readthedocs.projects.models.Project.checkout_path')
493+
def test_append_conf_existing_yaml_with_extra(self, checkout_path, run):
494+
tmpdir = tempfile.mkdtemp()
495+
os.mkdir(os.path.join(tmpdir, 'docs'))
496+
yaml_file = os.path.join(tmpdir, 'mkdocs.yml')
497+
yaml.safe_dump(
498+
{
499+
'site_name': 'mkdocs',
500+
'google_analytics': ['UA-1234-5', 'mkdocs.org'],
501+
'docs_dir': 'docs',
502+
'extra_css': [
503+
'http://readthedocs.org/static/css/badge_only.css'
504+
],
505+
'extra_javascript': ['readthedocs-data.js'],
506+
},
507+
open(yaml_file, 'w'),
508+
)
509+
checkout_path.return_value = tmpdir
510+
511+
python_env = Virtualenv(
512+
version=self.version,
513+
build_env=self.build_env,
514+
config=None,
515+
)
516+
self.searchbuilder = MkdocsHTML(
517+
build_env=self.build_env,
518+
python_env=python_env,
519+
)
520+
self.searchbuilder.append_conf()
521+
522+
run.assert_called_with('cat', 'mkdocs.yml', cwd=mock.ANY)
523+
524+
config = yaml.safe_load(open(yaml_file))
525+
526+
self.assertEqual(
527+
config['extra_css'],
528+
[
529+
'http://readthedocs.org/static/css/badge_only.css',
530+
'http://readthedocs.org/static/css/readthedocs-doc-embed.css',
531+
],
532+
)
533+
self.assertEqual(
534+
config['extra_javascript'],
535+
[
536+
'readthedocs-data.js',
537+
'http://readthedocs.org/static/core/js/readthedocs-doc-embed.js',
538+
'http://readthedocs.org/static/javascript/readthedocs-analytics.js',
539+
],
540+
)

0 commit comments

Comments
 (0)