From 632551069a96ba4e0617d36ce6486d912c826577 Mon Sep 17 00:00:00 2001 From: Michael Gnehr Date: Tue, 11 Jun 2019 12:04:47 +0200 Subject: [PATCH 01/17] add toc to wikipages and rendered md files see go-gitea#822 Signed-off-by: Michael Gnehr --- public/js/index.js | 92 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/public/js/index.js b/public/js/index.js index 28023e1061bbf..05a7def3cc9ed 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -3034,3 +3034,95 @@ function onOAuthLoginClick() { oauthNav.show(); },5000); } +(function() { + // html listings ---------------------------------------------------- + let openedLists, listEopen; + // close list + const _closeList = function (count) { + let out = ''; + if(count == false || count == 0 || count == 'undefined' || typeof(count) == 'undefined' ) { + count = openedLists.length; + } else { + count = Math.min(count, openedLists.length); + } + while (count > 0){ + out += '' + openedLists.pop(); + listEopen = true; + count--; + } + return out; + }; + // open list + const _openList = function () { + let out = ''); + listEopen = false; + return out; + }; + // handle list element + // create valid html list + const __list = function (line, level, id) { + let out = ''; + let diff = level - openedLists.length; + if(diff > 0) { //open new level + out += _openList(); + out += __list(line,level, id); + } else if(diff < 0 ) { + out += _closeList(-diff); + out += __list(line, level, id); + } else { // only add list element + out += ((listEopen)?'':'') + '
  • ' + line + ''; + listEopen = true; + } + return out; + }; + /** + * find headlines and create list ---------------------------------- + * @param target Element target container where toc should be created + */ + const create_toc_inside = function(target) { + let rm; + if(target != null) { + if( (rm = target.querySelector('.auto-toc-wrapper')) != null ) { + rm.parentNode.removeChild(rm); + } + openedLists = []; listEopen = false; + // get content and create html + const elms = target.querySelectorAll('h1,h2,h3,h4,h5'); + let html = ''; + for(let i = 0; i < elms.length; i++){ + let l = elms[i].tagName.substr(1); //level + let t = elms[i].innerText.trim().trim(''); //text + let id = elms[i].id; + // create html + if(t.length > 0 && l >= 1) { + html += __list( t, l, id); + } else { + html += _closeList(0) + l; + } + } + html += _closeList(0); + //create elements + let d = document.createElement('div'); + d.id = 'auto-toc'; + d.className = 'anchor-wrap'; + d.innerHTML = '

    Table of Contents

    '; + let d2 = document.createElement('div'); + d2.className = 'auto-toc-container'; + d2.innerHTML = html; + d2.insertBefore(d, d2.firstChild); + let c = document.createElement('div'); + c.className = 'auto-toc-wrapper'; + c.appendChild(d2); + //inject toc + target.insertBefore(c, target.firstChild); + //set style + c.style.cssText = "float:right;background:#fff;padding:0 0 7px 20px;position:relative;z-index:1"; + d2.style.cssText = "padding:7px;border:1px solid #333;border-radius:5px"; + } + }; + // create toc ---------------------------------- + create_toc_inside(document.querySelector('.file-view.markdown')); // md + create_toc_inside(document.querySelector('.segment.markdown')); // wiki pages +})(); + From 0a9fb89f497c3d893a6127fabd0e60bf98315593 Mon Sep 17 00:00:00 2001 From: Michael Gnehr Date: Tue, 11 Jun 2019 13:15:02 +0200 Subject: [PATCH 02/17] move style settings from js to css files Signed-off-by: Michael Gnehr --- public/css/index.css | 2 ++ public/css/theme-arc-green.css | 2 ++ public/js/index.js | 3 --- public/less/_markdown.less | 14 ++++++++++++++ public/less/themes/arc-green.less | 8 ++++++++ 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/public/css/index.css b/public/css/index.css index d192f43d1529a..a0841137d6a84 100644 --- a/public/css/index.css +++ b/public/css/index.css @@ -287,6 +287,8 @@ footer .ui.left,footer .ui.right{line-height:40px} .markdown:not(code) .csv-data tr{border-top:0} .markdown:not(code) .csv-data th{font-weight:700;background:#f8f8f8;border-top:0} .markdown:not(code) .ui.list .list,.markdown:not(code) ol.ui.list ol,.markdown:not(code) ul.ui.list ul{padding-left:2em} +.markdown:not(code) .auto-toc-wrapper{position:relative;padding:0 0 7px 20px;float:right;background:#fff;z-index:1} +.markdown:not(code) .auto-toc-container{padding:7px;border:1px solid #d4d4d5;border-radius:5px} .home .logo{max-width:220px} @media only screen and (max-width:767px){.home .hero h1{font-size:3.5em} .home .hero h2{font-size:2em} diff --git a/public/css/theme-arc-green.css b/public/css/theme-arc-green.css index dd2b13542a4de..21aa71c160f59 100644 --- a/public/css/theme-arc-green.css +++ b/public/css/theme-arc-green.css @@ -93,6 +93,8 @@ footer{background:#2e323e;border-top:1px solid #313131} .hljs,.hljs-keyword,.hljs-selector-tag,.hljs-subst{color:#9daccc} .markdown:not(code) .highlight pre,.markdown:not(code) pre{background-color:#2a2e3a;border:1px solid #404552} .markdown:not(code) table tr:nth-child(2n){background-color:#474d61} +.markdown:not(code) .auto-toc-wrapper{background:#353945} +.markdown:not(code) .auto-toc-container{background:#2a2e3a} .ui.dropdown .menu{background:#2c303a} .ui.dropdown .menu>.message:not(.ui){color:#636363} .ui.input{color:#dbdbdb} diff --git a/public/js/index.js b/public/js/index.js index 05a7def3cc9ed..df7dba690573e 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -3116,9 +3116,6 @@ function onOAuthLoginClick() { c.appendChild(d2); //inject toc target.insertBefore(c, target.firstChild); - //set style - c.style.cssText = "float:right;background:#fff;padding:0 0 7px 20px;position:relative;z-index:1"; - d2.style.cssText = "padding:7px;border:1px solid #333;border-radius:5px"; } }; // create toc ---------------------------------- diff --git a/public/less/_markdown.less b/public/less/_markdown.less index af46d6a3b21cf..dafde53a008b5 100644 --- a/public/less/_markdown.less +++ b/public/less/_markdown.less @@ -496,4 +496,18 @@ ul.ui.list ul { padding-left: 2em; } + + .auto-toc-wrapper { + position: relative; + padding: 0 0 7px 20px; + float: right; + background: #ffffff; + z-index: 1; + } + + .auto-toc-container { + padding: 7px; + border: 1px solid #d4d4d5; + border-radius: 5px; + } } diff --git a/public/less/themes/arc-green.less b/public/less/themes/arc-green.less index 6d13bb2e5217e..a79968f84455b 100644 --- a/public/less/themes/arc-green.less +++ b/public/less/themes/arc-green.less @@ -504,6 +504,14 @@ a.ui.basic.green.label:hover { background-color: #474d61; } +.markdown:not(code) .auto-toc-wrapper { + background: #353945; +} + +.markdown:not(code) .auto-toc-container { + background: #2a2e3a; +} + .ui.dropdown .menu { background: #2c303a; } From 0e08aa9f7e7bd56b0b4ecb241f44414814f0284f Mon Sep 17 00:00:00 2001 From: Michael Gnehr Date: Tue, 11 Jun 2019 13:29:43 +0200 Subject: [PATCH 03/17] fix dark theme border color Signed-off-by: Michael Gnehr --- public/css/theme-arc-green.css | 2 +- public/less/themes/arc-green.less | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/public/css/theme-arc-green.css b/public/css/theme-arc-green.css index 21aa71c160f59..c3f50589a234d 100644 --- a/public/css/theme-arc-green.css +++ b/public/css/theme-arc-green.css @@ -94,7 +94,7 @@ footer{background:#2e323e;border-top:1px solid #313131} .markdown:not(code) .highlight pre,.markdown:not(code) pre{background-color:#2a2e3a;border:1px solid #404552} .markdown:not(code) table tr:nth-child(2n){background-color:#474d61} .markdown:not(code) .auto-toc-wrapper{background:#353945} -.markdown:not(code) .auto-toc-container{background:#2a2e3a} +.markdown:not(code) .auto-toc-container{background:#2a2e3a;border-color:#404552} .ui.dropdown .menu{background:#2c303a} .ui.dropdown .menu>.message:not(.ui){color:#636363} .ui.input{color:#dbdbdb} diff --git a/public/less/themes/arc-green.less b/public/less/themes/arc-green.less index a79968f84455b..ad28d4c186578 100644 --- a/public/less/themes/arc-green.less +++ b/public/less/themes/arc-green.less @@ -510,6 +510,7 @@ a.ui.basic.green.label:hover { .markdown:not(code) .auto-toc-container { background: #2a2e3a; + border-color: #404552; } .ui.dropdown .menu { From 994bf331e3fa5f5d6f35b4cd270112aa633565d5 Mon Sep 17 00:00:00 2001 From: Michael Gnehr Date: Wed, 12 Jun 2019 16:30:04 +0200 Subject: [PATCH 04/17] change padding on TOC headlines Signed-off-by: Michael Gnehr --- public/css/index.css | 1 + public/less/_markdown.less | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/public/css/index.css b/public/css/index.css index a0841137d6a84..eb2c78da5d666 100644 --- a/public/css/index.css +++ b/public/css/index.css @@ -289,6 +289,7 @@ footer .ui.left,footer .ui.right{line-height:40px} .markdown:not(code) .ui.list .list,.markdown:not(code) ol.ui.list ol,.markdown:not(code) ul.ui.list ul{padding-left:2em} .markdown:not(code) .auto-toc-wrapper{position:relative;padding:0 0 7px 20px;float:right;background:#fff;z-index:1} .markdown:not(code) .auto-toc-container{padding:7px;border:1px solid #d4d4d5;border-radius:5px} +.markdown:not(code) .auto-toc-container h2{padding:.3em;font-size:1.65em} .home .logo{max-width:220px} @media only screen and (max-width:767px){.home .hero h1{font-size:3.5em} .home .hero h2{font-size:2em} diff --git a/public/less/_markdown.less b/public/less/_markdown.less index dafde53a008b5..44a3cc80ef3df 100644 --- a/public/less/_markdown.less +++ b/public/less/_markdown.less @@ -509,5 +509,10 @@ padding: 7px; border: 1px solid #d4d4d5; border-radius: 5px; + + h2 { + padding: 0.3em; + font-size: 1.65em; + } } } From f60056bdbb206c4773d14aea303ad0791fdccaa9 Mon Sep 17 00:00:00 2001 From: Michael Gnehr Date: Wed, 12 Jun 2019 18:12:18 +0200 Subject: [PATCH 05/17] add md and wikifile TOC settings to repository options Signed-off-by: Michael Gnehr --- custom/conf/app.ini.sample | 6 +++ .../doc/advanced/config-cheat-sheet.en-us.md | 6 +++ models/migrations/migrations.go | 2 + models/migrations/v88.go | 40 +++++++++++++++++++ models/repo.go | 6 +++ modules/auth/repo_form.go | 3 ++ modules/setting/repository.go | 6 +++ options/locale/locale_de-DE.ini | 4 ++ options/locale/locale_en-US.ini | 4 ++ routers/repo/setting.go | 18 +++++++++ templates/repo/settings/options.tmpl | 25 ++++++++++++ 11 files changed, 120 insertions(+) create mode 100644 models/migrations/v88.go diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample index a674984a2584e..010862d8a4e01 100644 --- a/custom/conf/app.ini.sample +++ b/custom/conf/app.ini.sample @@ -39,6 +39,12 @@ ACCESS_CONTROL_ALLOW_ORIGIN = USE_COMPAT_SSH_URI = false ; Close issues as long as a commit on any branch marks it as fixed DEFAULT_CLOSE_ISSUES_VIA_COMMITS_IN_ANY_BRANCH = false +; Create Table of Contents based on headlines on wiki page (does not alter file) +DEFAULT_TOC_WIKI_FILE = true +; Create Table of Contents on all rendered markdown files (does not alter file), enabled: overwrites DEFAULT_TOC_MARKDOWN_BY_FLAG +DEFAULT_TOC_MARKDOWN_ALWAYS = false +; Create Table of Contents on renderes markdown files if line '%%TOC%%' is present +DEFAULT_TOC_MARKDOWN_BY_FLAG = true [repository.editor] ; List of file extensions for which lines should be wrapped in the CodeMirror editor diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index ecc196c86e136..f1a2289c64780 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -66,6 +66,12 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`. default is not to present. **WARNING**: This maybe harmful to you website if you do not give it a right value. - `DEFAULT_CLOSE_ISSUES_VIA_COMMITS_IN_ANY_BRANCH`: **false**: Close an issue if a commit on a non default branch marks it as closed. +- `DEFAULT_TOC_WIKI_FILE`: **true**: Create Table of Contents based on headlines on wiki page (does + not alter file) +- `DEFAULT_TOC_MARKDOWN_ALWAYS`: **false** : Create Table of Contents on all rendered markdown files + (does not alter file), enabled: overwrites `TOC_MARKDOWN_BY_FLAG` +- `DEFAULT_TOC_MARKDOWN_BY_FLAG`: **true** : Create Table of Contents on renderes markdown files if + line '%%TOC%%' is present ### Repository - Pull Request (`repository.pull-request`) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index b95a74c3621db..c5ad549c5d0bc 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -229,6 +229,8 @@ var migrations = []Migration{ NewMigration("add http method to webhook", addHTTPMethodToWebhook), // v87 -> v88 NewMigration("add avatar field to repository", addAvatarFieldToRepository), + // v88 -> v89 + NewMigration("add toc on wiki and markedown", addCanTocOnWikiAndMarkdown), } // Migrate database to current version diff --git a/models/migrations/v88.go b/models/migrations/v88.go new file mode 100644 index 0000000000000..efb3403969ebe --- /dev/null +++ b/models/migrations/v88.go @@ -0,0 +1,40 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package migrations + +import ( + "code.gitea.io/gitea/modules/setting" + + "github.com/go-xorm/xorm" +) + +func addCanTocOnWikiAndMarkdown(x *xorm.Engine) error { + + type Repository struct { + TocWikiFile bool `xorm:"NOT NULL DEFAULT true"` + TocMarkdownAlways bool `xorm:"NOT NULL DEFAULT false"` + TocMarkdownByFlag bool `xorm:"NOT NULL DEFAULT true"` + } + + if err := x.Sync2(new(Repository)); err != nil { + return err + } + + if _, err := x.Exec("UPDATE repository SET toc_wiki_file = ?", + setting.Repository.DefaultTocWikiFile); err != nil { + return err + } + + if _, err := x.Exec("UPDATE repository SET toc_markdown_always = ?", + setting.Repository.DefaultTocMarkdownAlways); err != nil { + return err + } + + if _, err := x.Exec("UPDATE repository SET toc_markdown_by_flag = ?", + setting.Repository.DefaultTocMarkdownByFlag); err != nil { + return err + } + return nil +} diff --git a/models/repo.go b/models/repo.go index d5eca3d22502e..2b535225b1226 100644 --- a/models/repo.go +++ b/models/repo.go @@ -170,6 +170,9 @@ type Repository struct { IndexerStatus *RepoIndexerStatus `xorm:"-"` IsFsckEnabled bool `xorm:"NOT NULL DEFAULT true"` CloseIssuesViaCommitInAnyBranch bool `xorm:"NOT NULL DEFAULT false"` + TocWikiFile bool `xorm:"NOT NULL DEFAULT true"` + TocMarkdownAlways bool `xorm:"NOT NULL DEFAULT false"` + TocMarkdownByFlag bool `xorm:"NOT NULL DEFAULT true"` Topics []string `xorm:"TEXT JSON"` // Avatar: ID(10-20)-md5(32) - must fit into 64 symbols @@ -1371,6 +1374,9 @@ func CreateRepository(doer, u *User, opts CreateRepoOptions) (_ *Repository, err IsPrivate: opts.IsPrivate, IsFsckEnabled: !opts.IsMirror, CloseIssuesViaCommitInAnyBranch: setting.Repository.DefaultCloseIssuesViaCommitsInAnyBranch, + TocWikiFile: setting.Repository.DefaultTocWikiFile, + TocMarkdownAlways: setting.Repository.DefaultTocMarkdownAlways, + TocMarkdownByFlag: setting.Repository.DefaultTocMarkdownByFlag, } sess := x.NewSession() diff --git a/modules/auth/repo_form.go b/modules/auth/repo_form.go index 0333c3c92614a..f9302c2b24b08 100644 --- a/modules/auth/repo_form.go +++ b/modules/auth/repo_form.go @@ -125,6 +125,9 @@ type RepoSettingForm struct { AllowOnlyContributorsToTrackTime bool EnableIssueDependencies bool IsArchived bool + TocWikiFile bool + TocMarkdownAlways bool + TocMarkdownByFlag bool // Admin settings EnableHealthCheck bool diff --git a/modules/setting/repository.go b/modules/setting/repository.go index 98e3d6e82624d..52a90e30f41e7 100644 --- a/modules/setting/repository.go +++ b/modules/setting/repository.go @@ -34,6 +34,9 @@ var ( AccessControlAllowOrigin string UseCompatSSHURI bool DefaultCloseIssuesViaCommitsInAnyBranch bool + DefaultTocWikiFile bool + DefaultTocMarkdownAlways bool + DefaultTocMarkdownByFlag bool // Repository editor settings Editor struct { @@ -76,6 +79,9 @@ var ( AccessControlAllowOrigin: "", UseCompatSSHURI: false, DefaultCloseIssuesViaCommitsInAnyBranch: false, + DefaultTocWikiFile: true, + DefaultTocMarkdownAlways: false, + DefaultTocMarkdownByFlag: true, // Repository editor settings Editor: struct { diff --git a/options/locale/locale_de-DE.ini b/options/locale/locale_de-DE.ini index 7176b23f56040..6966535f5b9a4 100644 --- a/options/locale/locale_de-DE.ini +++ b/options/locale/locale_de-DE.ini @@ -1147,6 +1147,10 @@ settings.pulls.allow_squash_commits=Mergen von Commits durch Squash aktivieren settings.admin_settings=Administratoreinstellungen settings.admin_enable_health_check=Repository-Health-Checks aktivieren (git fsck) settings.admin_enable_close_issues_via_commit_in_any_branch=Einen Issue mit einem Commit auf einem nicht-Standard-Branch schließen +settings.toc.toc = Inhaltsverzeichnis +settings.toc.toc_wiki_file = Automatisches Inhaltsverzeichnis auf Wikiseiten rendern. +settings.toc.toc_markdown_always = Automatisches Inhaltsverzeichnis auf allen Markdown Seiten rendern. +settings.toc.toc_markdown_by_flag = Automatisches Inhaltsverzeichnis auf Markdown Seiten rendern, wenn diese das Flag '%%TOC%%' enthalten. settings.danger_zone=Gefahrenzone settings.new_owner_has_same_repo=Der neue Eigentümer hat bereits ein Repository mit dem gleichen Namen. Bitte wähle einen anderen Namen. settings.convert=In ein normales Repository umwandeln diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 71c76fd9b6ada..f398228ad3acd 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1149,6 +1149,10 @@ settings.pulls.allow_squash_commits = Enable Squashing to Merge Commits settings.admin_settings = Administrator Settings settings.admin_enable_health_check = Enable Repository Health Checks (git fsck) settings.admin_enable_close_issues_via_commit_in_any_branch = Close an issue via a commit made in a non default branch +settings.toc.toc = Table of Contents +settings.toc.toc_wiki_file = Enable autogenerated TOC on wikipages (based on headlines) (does not alter files) +settings.toc.toc_markdown_always = Enable autogenerated TOC on all markdown files (based on headlines) (does not alter files) +settings.toc.toc_markdown_by_flag = Enable autogenerated TOC on markdown files which contain '%%TOC%%' (based on headlines) (does not alter files) settings.danger_zone = Danger Zone settings.new_owner_has_same_repo = The new owner already has a repository with same name. Please choose another name. settings.convert = Convert to Regular Repository diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 767cdacde0195..741c39e807b82 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -294,6 +294,24 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { ctx.ServerError("UpdateRepositoryUnits", err) return } + + if repo.TocWikiFile != form.TocWikiFile { + repo.TocWikiFile = form.TocWikiFile + } + + if repo.TocMarkdownAlways != form.TocMarkdownAlways { + repo.TocMarkdownAlways = form.TocMarkdownAlways + } + + if repo.TocMarkdownByFlag != form.TocMarkdownByFlag { + repo.TocMarkdownByFlag = form.TocMarkdownByFlag + } + + if err := models.UpdateRepository(repo, false); err != nil { + ctx.ServerError("UpdateRepository", err) + return + } + log.Trace("Repository advanced settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name) ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success")) diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index c6d715acbee88..dd6fc4b9e074f 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -262,6 +262,31 @@ {{end}} +
    + +
    +
    + +
    +
    + + +
    +
    +
    +
    + + +
    +
    +
    +
    + + +
    +
    +
    +
    From a9dce7f38cc9230174296ce71e730b87ba93be75 Mon Sep 17 00:00:00 2001 From: Michael Gnehr Date: Wed, 12 Jun 2019 21:33:36 +0200 Subject: [PATCH 06/17] handle options on template and update js Signed-off-by: Michael Gnehr --- public/js/index.js | 103 ++++++++++++++++++++++++---------- templates/repo/view_file.tmpl | 2 +- templates/repo/wiki/view.tmpl | 2 +- 3 files changed, 74 insertions(+), 33 deletions(-) diff --git a/public/js/index.js b/public/js/index.js index df7dba690573e..ca5f0017b0a08 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -3076,6 +3076,42 @@ function onOAuthLoginClick() { } return out; }; + /** + * find headlines and create list ---------------------------------- + * @param target Element target container where toc should look for headlines + * @return Element toc wrapper - div container + */ + const get_toc_inside = function (target) { + openedLists = []; listEopen = false; + // get content and create html + const elms = target.querySelectorAll('h1,h2,h3,h4,h5'); + let html = ''; + for(let i = 0; i < elms.length; i++){ + let l = elms[i].tagName.substr(1); //level + let t = elms[i].innerText.trim().trim(''); //text + let id = elms[i].id; + // create html + if(t.length > 0 && l >= 1) { + html += __list( t, l, id); + } else { + html += _closeList(0) + l; + } + } + html += _closeList(0); + //create elements + let d = document.createElement('div'); + d.id = 'auto-toc'; + d.className = 'anchor-wrap'; + d.innerHTML = '

    '+((typeof(target.dataset.toc) == 'string' && target.dataset.toc != '')?target.dataset.toc:'Table of Contents')+'

    '; + let d2 = document.createElement('div'); + d2.className = 'auto-toc-container'; + d2.innerHTML = html; + d2.insertBefore(d, d2.firstChild); + let c = document.createElement('div'); + c.className = 'auto-toc-wrapper'; + c.appendChild(d2); + return c; + }; /** * find headlines and create list ---------------------------------- * @param target Element target container where toc should be created @@ -3086,40 +3122,45 @@ function onOAuthLoginClick() { if( (rm = target.querySelector('.auto-toc-wrapper')) != null ) { rm.parentNode.removeChild(rm); } - openedLists = []; listEopen = false; - // get content and create html - const elms = target.querySelectorAll('h1,h2,h3,h4,h5'); - let html = ''; - for(let i = 0; i < elms.length; i++){ - let l = elms[i].tagName.substr(1); //level - let t = elms[i].innerText.trim().trim(''); //text - let id = elms[i].id; - // create html - if(t.length > 0 && l >= 1) { - html += __list( t, l, id); - } else { - html += _closeList(0) + l; - } + //remove optional toc flag + let ps = target.querySelectorAll('p'); + //look for toc keywoard + for (let i = 0; i < ps.length; i++) { + if (ps[i].textContent.trim() == '%%TOC%%') { + ps[i].parentNode.removeChild(ps[i]); + } } - html += _closeList(0); - //create elements - let d = document.createElement('div'); - d.id = 'auto-toc'; - d.className = 'anchor-wrap'; - d.innerHTML = '

    Table of Contents

    '; - let d2 = document.createElement('div'); - d2.className = 'auto-toc-container'; - d2.innerHTML = html; - d2.insertBefore(d, d2.firstChild); - let c = document.createElement('div'); - c.className = 'auto-toc-wrapper'; - c.appendChild(d2); //inject toc - target.insertBefore(c, target.firstChild); + target.insertBefore(get_toc_inside(target), target.firstChild); + } + }; + /** + * search for %%TOC%% inside document if found create toc -------------------------- + * @param target Element target container where toc should be created + */ + const detect_toc_flag = function(target) { + if(target != null) { + let ps = target.querySelectorAll('p'); + let found = false; + //look for toc keywoard + for (let i = 0; i < ps.length; i++) { + if (ps[i].textContent.trim() == '%%TOC%%') { + found = ps[i]; + break; + } + } + if(found !== false) { + //remove toc keywoard + found.parentNode.removeChild(found); + //create toc + create_toc_inside(target); + } } }; // create toc ---------------------------------- - create_toc_inside(document.querySelector('.file-view.markdown')); // md - create_toc_inside(document.querySelector('.segment.markdown')); // wiki pages + addEventListener("load", function(){ + create_toc_inside(document.querySelector('.file-view.markdown.auto-toc')); // md + detect_toc_flag( document.querySelector('.file-view.markdown.auto-toc-by-flag')); // md by %%TOC%% flag + create_toc_inside(document.querySelector('.segment.markdown.auto-toc')); // wiki pages + }); })(); - diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 6445ee7b9e58d..56734e6030da2 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -45,7 +45,7 @@
    -
    +
    {{if .IsMarkup}} {{if .FileContent}}{{.FileContent | Safe}}{{end}} {{else if .IsRenderedHTML}} diff --git a/templates/repo/wiki/view.tmpl b/templates/repo/wiki/view.tmpl index dd2de2a041380..d276d7acfb08b 100644 --- a/templates/repo/wiki/view.tmpl +++ b/templates/repo/wiki/view.tmpl @@ -79,7 +79,7 @@
    {{end}}
    -
    +
    {{.content | Str2html}}
    {{if .sidebarPresent}} From e971230c01e967485d6af4095e31a812a180a0cd Mon Sep 17 00:00:00 2001 From: Cherrg Date: Thu, 13 Jun 2019 02:45:07 +0200 Subject: [PATCH 07/17] Update index.js --- public/js/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/js/index.js b/public/js/index.js index ca5f0017b0a08..11cdd2e5c810b 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -3094,7 +3094,7 @@ function onOAuthLoginClick() { if(t.length > 0 && l >= 1) { html += __list( t, l, id); } else { - html += _closeList(0) + l; + html += _closeList(0); } } html += _closeList(0); From a889262a4c1bf9d618173eb8b9dc3a3a64bd6c81 Mon Sep 17 00:00:00 2001 From: Michael Gnehr Date: Thu, 13 Jun 2019 03:47:14 +0200 Subject: [PATCH 08/17] add float clear element + reduce js call stack: function -> inline function Signed-off-by: Michael Gnehr --- public/css/index.css | 1 + public/js/index.js | 75 +++++++++++++++++++------------------- public/less/_markdown.less | 4 ++ 3 files changed, 42 insertions(+), 38 deletions(-) diff --git a/public/css/index.css b/public/css/index.css index eb2c78da5d666..654e2f085a90d 100644 --- a/public/css/index.css +++ b/public/css/index.css @@ -290,6 +290,7 @@ footer .ui.left,footer .ui.right{line-height:40px} .markdown:not(code) .auto-toc-wrapper{position:relative;padding:0 0 7px 20px;float:right;background:#fff;z-index:1} .markdown:not(code) .auto-toc-container{padding:7px;border:1px solid #d4d4d5;border-radius:5px} .markdown:not(code) .auto-toc-container h2{padding:.3em;font-size:1.65em} +.markdown:not(code) .auto-toc-clear{clear:both} .home .logo{max-width:220px} @media only screen and (max-width:767px){.home .hero h1{font-size:3.5em} .home .hero h2{font-size:2em} diff --git a/public/js/index.js b/public/js/index.js index 11cdd2e5c810b..653f79136f3fe 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -3076,42 +3076,6 @@ function onOAuthLoginClick() { } return out; }; - /** - * find headlines and create list ---------------------------------- - * @param target Element target container where toc should look for headlines - * @return Element toc wrapper - div container - */ - const get_toc_inside = function (target) { - openedLists = []; listEopen = false; - // get content and create html - const elms = target.querySelectorAll('h1,h2,h3,h4,h5'); - let html = ''; - for(let i = 0; i < elms.length; i++){ - let l = elms[i].tagName.substr(1); //level - let t = elms[i].innerText.trim().trim(''); //text - let id = elms[i].id; - // create html - if(t.length > 0 && l >= 1) { - html += __list( t, l, id); - } else { - html += _closeList(0); - } - } - html += _closeList(0); - //create elements - let d = document.createElement('div'); - d.id = 'auto-toc'; - d.className = 'anchor-wrap'; - d.innerHTML = '

    '+((typeof(target.dataset.toc) == 'string' && target.dataset.toc != '')?target.dataset.toc:'Table of Contents')+'

    '; - let d2 = document.createElement('div'); - d2.className = 'auto-toc-container'; - d2.innerHTML = html; - d2.insertBefore(d, d2.firstChild); - let c = document.createElement('div'); - c.className = 'auto-toc-wrapper'; - c.appendChild(d2); - return c; - }; /** * find headlines and create list ---------------------------------- * @param target Element target container where toc should be created @@ -3130,8 +3094,43 @@ function onOAuthLoginClick() { ps[i].parentNode.removeChild(ps[i]); } } - //inject toc - target.insertBefore(get_toc_inside(target), target.firstChild); + openedLists = []; listEopen = false; + // get content and create html + const elms = target.querySelectorAll('h1,h2,h3,h4,h5'); + if (elms.length > 0) { + let html = ''; + for(let i = 0; i < elms.length; i++){ + let l = elms[i].tagName.substr(1); //level + let t = elms[i].innerText.trim().trim(''); //text + let id = elms[i].id; + // create html + if(t.length > 0 && l >= 1) { + html += __list(t, l, id); + } else { + html += _closeList(0); + } + } + html += _closeList(0); + //create elements + let d = document.createElement('div'); + d.id = 'auto-toc'; + d.className = 'anchor-wrap'; + d.innerHTML = '

    '+((typeof(target.dataset.toc) == 'string' && target.dataset.toc != '')?target.dataset.toc:'Table of Contents')+'

    '; + let d2 = document.createElement('div'); + d2.className = 'auto-toc-container'; + d2.innerHTML = html; + d2.insertBefore(d, d2.firstChild); + let c = document.createElement('div'); + c.className = 'auto-toc-wrapper'; + c.appendChild(d2); + //inject toc + target.insertBefore(c, target.firstChild); + if( (rm = document.querySelector('.auto-toc-clear')) != null ) { + rm.parentNode.removeChild(rm); + } + let a = document.createElement('div'); a.className = 'auto-toc-clear'; + target.appendChild(a); + } } }; /** diff --git a/public/less/_markdown.less b/public/less/_markdown.less index 44a3cc80ef3df..f12b70b4fc860 100644 --- a/public/less/_markdown.less +++ b/public/less/_markdown.less @@ -515,4 +515,8 @@ font-size: 1.65em; } } + + .auto-toc-clear { + clear: both; + } } From 77fb62127e6e350db28a68fae624f0c61f5fbe25 Mon Sep 17 00:00:00 2001 From: Michael Gnehr Date: Thu, 13 Jun 2019 04:48:19 +0200 Subject: [PATCH 09/17] move TOC settings in app.ini from section 'repository' to 'markdown' Signed-off-by: Michael Gnehr --- custom/conf/app.ini.sample | 12 ++++++------ .../doc/advanced/config-cheat-sheet.en-us.md | 12 ++++++------ models/migrations/v88.go | 6 +++--- models/repo.go | 6 +++--- modules/setting/repository.go | 6 ------ modules/setting/setting.go | 16 +++++++++++----- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample index 010862d8a4e01..bb7eac824a40b 100644 --- a/custom/conf/app.ini.sample +++ b/custom/conf/app.ini.sample @@ -39,12 +39,6 @@ ACCESS_CONTROL_ALLOW_ORIGIN = USE_COMPAT_SSH_URI = false ; Close issues as long as a commit on any branch marks it as fixed DEFAULT_CLOSE_ISSUES_VIA_COMMITS_IN_ANY_BRANCH = false -; Create Table of Contents based on headlines on wiki page (does not alter file) -DEFAULT_TOC_WIKI_FILE = true -; Create Table of Contents on all rendered markdown files (does not alter file), enabled: overwrites DEFAULT_TOC_MARKDOWN_BY_FLAG -DEFAULT_TOC_MARKDOWN_ALWAYS = false -; Create Table of Contents on renderes markdown files if line '%%TOC%%' is present -DEFAULT_TOC_MARKDOWN_BY_FLAG = true [repository.editor] ; List of file extensions for which lines should be wrapped in the CodeMirror editor @@ -151,6 +145,12 @@ CUSTOM_URL_SCHEMES = ; List of file extensions that should be rendered/edited as Markdown ; Separate the extensions with a comma. To render files without any extension as markdown, just put a comma FILE_EXTENSIONS = .md,.markdown,.mdown,.mkd +; Create Table of Contents based on headlines on wiki page (does not alter file) +DEFAULT_TOC_WIKI_FILE = true +; Create Table of Contents on all rendered markdown files (does not alter file), enabled: overwrites DEFAULT_TOC_MARKDOWN_BY_FLAG +DEFAULT_TOC_MARKDOWN_ALWAYS = false +; Create Table of Contents on renderes markdown files if line '%%TOC%%' is present +DEFAULT_TOC_MARKDOWN_BY_FLAG = true [server] ; The protocol the server listens on. One of 'http', 'https', 'unix' or 'fcgi'. diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index f1a2289c64780..76d923a4a929f 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -66,12 +66,6 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`. default is not to present. **WARNING**: This maybe harmful to you website if you do not give it a right value. - `DEFAULT_CLOSE_ISSUES_VIA_COMMITS_IN_ANY_BRANCH`: **false**: Close an issue if a commit on a non default branch marks it as closed. -- `DEFAULT_TOC_WIKI_FILE`: **true**: Create Table of Contents based on headlines on wiki page (does - not alter file) -- `DEFAULT_TOC_MARKDOWN_ALWAYS`: **false** : Create Table of Contents on all rendered markdown files - (does not alter file), enabled: overwrites `TOC_MARKDOWN_BY_FLAG` -- `DEFAULT_TOC_MARKDOWN_BY_FLAG`: **true** : Create Table of Contents on renderes markdown files if - line '%%TOC%%' is present ### Repository - Pull Request (`repository.pull-request`) @@ -113,6 +107,12 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`. ## Markdown (`markdown`) - `ENABLE_HARD_LINE_BREAK`: **false**: Enable Markdown's hard line break extension. +- `DEFAULT_TOC_WIKI_FILE`: **true**: Create Table of Contents based on headlines on wiki page (does + not alter file) +- `DEFAULT_TOC_MARKDOWN_ALWAYS`: **false** : Create Table of Contents on all rendered markdown files + (does not alter file), enabled: overwrites `TOC_MARKDOWN_BY_FLAG` +- `DEFAULT_TOC_MARKDOWN_BY_FLAG`: **true** : Create Table of Contents on renderes markdown files if + line '%%TOC%%' is present ## Server (`server`) diff --git a/models/migrations/v88.go b/models/migrations/v88.go index efb3403969ebe..a7228d224a82d 100644 --- a/models/migrations/v88.go +++ b/models/migrations/v88.go @@ -23,17 +23,17 @@ func addCanTocOnWikiAndMarkdown(x *xorm.Engine) error { } if _, err := x.Exec("UPDATE repository SET toc_wiki_file = ?", - setting.Repository.DefaultTocWikiFile); err != nil { + setting.Markdown.DefaultTocWikiFile); err != nil { return err } if _, err := x.Exec("UPDATE repository SET toc_markdown_always = ?", - setting.Repository.DefaultTocMarkdownAlways); err != nil { + setting.Markdown.DefaultTocMarkdownAlways); err != nil { return err } if _, err := x.Exec("UPDATE repository SET toc_markdown_by_flag = ?", - setting.Repository.DefaultTocMarkdownByFlag); err != nil { + setting.Markdown.DefaultTocMarkdownByFlag); err != nil { return err } return nil diff --git a/models/repo.go b/models/repo.go index 2b535225b1226..a73074c52043b 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1374,9 +1374,9 @@ func CreateRepository(doer, u *User, opts CreateRepoOptions) (_ *Repository, err IsPrivate: opts.IsPrivate, IsFsckEnabled: !opts.IsMirror, CloseIssuesViaCommitInAnyBranch: setting.Repository.DefaultCloseIssuesViaCommitsInAnyBranch, - TocWikiFile: setting.Repository.DefaultTocWikiFile, - TocMarkdownAlways: setting.Repository.DefaultTocMarkdownAlways, - TocMarkdownByFlag: setting.Repository.DefaultTocMarkdownByFlag, + TocWikiFile: setting.Markdown.DefaultTocWikiFile, + TocMarkdownAlways: setting.Markdown.DefaultTocMarkdownAlways, + TocMarkdownByFlag: setting.Markdown.DefaultTocMarkdownByFlag, } sess := x.NewSession() diff --git a/modules/setting/repository.go b/modules/setting/repository.go index 52a90e30f41e7..98e3d6e82624d 100644 --- a/modules/setting/repository.go +++ b/modules/setting/repository.go @@ -34,9 +34,6 @@ var ( AccessControlAllowOrigin string UseCompatSSHURI bool DefaultCloseIssuesViaCommitsInAnyBranch bool - DefaultTocWikiFile bool - DefaultTocMarkdownAlways bool - DefaultTocMarkdownByFlag bool // Repository editor settings Editor struct { @@ -79,9 +76,6 @@ var ( AccessControlAllowOrigin: "", UseCompatSSHURI: false, DefaultCloseIssuesViaCommitsInAnyBranch: false, - DefaultTocWikiFile: true, - DefaultTocMarkdownAlways: false, - DefaultTocMarkdownByFlag: true, // Repository editor settings Editor: struct { diff --git a/modules/setting/setting.go b/modules/setting/setting.go index ff53e9a3757f1..87ac2eb72b980 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -236,12 +236,18 @@ var ( // Markdown settings Markdown = struct { - EnableHardLineBreak bool - CustomURLSchemes []string `ini:"CUSTOM_URL_SCHEMES"` - FileExtensions []string + EnableHardLineBreak bool + CustomURLSchemes []string `ini:"CUSTOM_URL_SCHEMES"` + FileExtensions []string + DefaultTocWikiFile bool + DefaultTocMarkdownAlways bool + DefaultTocMarkdownByFlag bool }{ - EnableHardLineBreak: false, - FileExtensions: strings.Split(".md,.markdown,.mdown,.mkd", ","), + EnableHardLineBreak: false, + FileExtensions: strings.Split(".md,.markdown,.mdown,.mkd", ","), + DefaultTocWikiFile: true, + DefaultTocMarkdownAlways: false, + DefaultTocMarkdownByFlag: true, } // Admin settings From 06238aa35571b51963ca61356b4aadafda25d562 Mon Sep 17 00:00:00 2001 From: Cherrg Date: Thu, 13 Jun 2019 13:02:38 +0200 Subject: [PATCH 10/17] remove german translation --- options/locale/locale_de-DE.ini | 4 ---- 1 file changed, 4 deletions(-) diff --git a/options/locale/locale_de-DE.ini b/options/locale/locale_de-DE.ini index 6966535f5b9a4..7176b23f56040 100644 --- a/options/locale/locale_de-DE.ini +++ b/options/locale/locale_de-DE.ini @@ -1147,10 +1147,6 @@ settings.pulls.allow_squash_commits=Mergen von Commits durch Squash aktivieren settings.admin_settings=Administratoreinstellungen settings.admin_enable_health_check=Repository-Health-Checks aktivieren (git fsck) settings.admin_enable_close_issues_via_commit_in_any_branch=Einen Issue mit einem Commit auf einem nicht-Standard-Branch schließen -settings.toc.toc = Inhaltsverzeichnis -settings.toc.toc_wiki_file = Automatisches Inhaltsverzeichnis auf Wikiseiten rendern. -settings.toc.toc_markdown_always = Automatisches Inhaltsverzeichnis auf allen Markdown Seiten rendern. -settings.toc.toc_markdown_by_flag = Automatisches Inhaltsverzeichnis auf Markdown Seiten rendern, wenn diese das Flag '%%TOC%%' enthalten. settings.danger_zone=Gefahrenzone settings.new_owner_has_same_repo=Der neue Eigentümer hat bereits ein Repository mit dem gleichen Namen. Bitte wähle einen anderen Namen. settings.convert=In ein normales Repository umwandeln From c486aca2c8b9f4f000325c73d5153dfeb26d026a Mon Sep 17 00:00:00 2001 From: Michael Gnehr Date: Thu, 13 Jun 2019 17:00:54 +0200 Subject: [PATCH 11/17] add configuration flag to set TOC extensions Signed-off-by: Michael Gnehr --- custom/conf/app.ini.sample | 3 ++ .../doc/advanced/config-cheat-sheet.en-us.md | 3 ++ modules/setting/setting.go | 24 +++++++------ routers/repo/view.go | 35 +++++++++++++++++++ templates/repo/view_file.tmpl | 2 +- 5 files changed, 55 insertions(+), 12 deletions(-) diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample index bb7eac824a40b..372f362601f85 100644 --- a/custom/conf/app.ini.sample +++ b/custom/conf/app.ini.sample @@ -151,6 +151,9 @@ DEFAULT_TOC_WIKI_FILE = true DEFAULT_TOC_MARKDOWN_ALWAYS = false ; Create Table of Contents on renderes markdown files if line '%%TOC%%' is present DEFAULT_TOC_MARKDOWN_BY_FLAG = true +; List of markup file extensions that TOC should be created on, set to not interfere with external renderers +; Separate the extensions with a comma. To ignore file extension check, just put a comma +TOC_MARKDOWN_FILE_EXTENSIONS = .md,.markdown,.mdown,.mkd,.org [server] ; The protocol the server listens on. One of 'http', 'https', 'unix' or 'fcgi'. diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index 76d923a4a929f..1d346d6ff8082 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -113,6 +113,9 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`. (does not alter file), enabled: overwrites `TOC_MARKDOWN_BY_FLAG` - `DEFAULT_TOC_MARKDOWN_BY_FLAG`: **true** : Create Table of Contents on renderes markdown files if line '%%TOC%%' is present +- `TOC_MARKDOWN_FILE_EXTENSIONS`: **.md,.markdown,.mdown,.mkd,.org** : List of markup file extensions + that TOC should be created on, set to not interfere with external renderers. + Separate the extensions with a comma. To ignore file extension check, just put a comma. ## Server (`server`) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 87ac2eb72b980..b9975cf5ef8dd 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -236,18 +236,20 @@ var ( // Markdown settings Markdown = struct { - EnableHardLineBreak bool - CustomURLSchemes []string `ini:"CUSTOM_URL_SCHEMES"` - FileExtensions []string - DefaultTocWikiFile bool - DefaultTocMarkdownAlways bool - DefaultTocMarkdownByFlag bool + EnableHardLineBreak bool + CustomURLSchemes []string `ini:"CUSTOM_URL_SCHEMES"` + FileExtensions []string + DefaultTocWikiFile bool + DefaultTocMarkdownAlways bool + DefaultTocMarkdownByFlag bool + TocMarkdownFileExtensions []string }{ - EnableHardLineBreak: false, - FileExtensions: strings.Split(".md,.markdown,.mdown,.mkd", ","), - DefaultTocWikiFile: true, - DefaultTocMarkdownAlways: false, - DefaultTocMarkdownByFlag: true, + EnableHardLineBreak: false, + FileExtensions: strings.Split(".md,.markdown,.mdown,.mkd", ","), + DefaultTocWikiFile: true, + DefaultTocMarkdownAlways: false, + DefaultTocMarkdownByFlag: true, + TocMarkdownFileExtensions: strings.Split(".md,.markdown,.mdown,.mkd,.org", ","), } // Admin settings diff --git a/routers/repo/view.go b/routers/repo/view.go index 3483a53a0d31f..1aba934d80a2b 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -12,6 +12,7 @@ import ( gotemplate "html/template" "io/ioutil" "path" + "path/filepath" "strings" "code.gitea.io/gitea/models" @@ -163,6 +164,23 @@ func renderDirectory(ctx *context.Context, treeLink string) { buf = templates.ToUTF8WithFallback(append(buf, d...)) if markup.Type(readmeFile.Name()) != "" { + // Check if extension matches TOC file list + tocExts := setting.Markdown.TocMarkdownFileExtensions + isTocMarkup := false + if len(tocExts) == 0 || len(tocExts) == 1 && tocExts[0] == "" || len(tocExts) == 2 && tocExts[0] == "" && tocExts[1] == "" { + isTocMarkup = true + } else { + fileExt := strings.ToLower(filepath.Ext(readmeFile.Name())) + if fileExt != "" { + for _, tExt := range tocExts { + if tExt == fileExt { + isTocMarkup = true + break + } + } + } + } + ctx.Data["IsTocMarkup"] = isTocMarkup ctx.Data["IsMarkup"] = true ctx.Data["FileContent"] = string(markup.Render(readmeFile.Name(), buf, treeLink, ctx.Repo.Repository.ComposeMetas())) } else { @@ -283,6 +301,23 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st readmeExist := markup.IsReadmeFile(blob.Name()) ctx.Data["ReadmeExist"] = readmeExist if markup.Type(blob.Name()) != "" { + // Check if extension matches TOC file list + tocExts := setting.Markdown.TocMarkdownFileExtensions + isTocMarkup := false + if len(tocExts) == 0 || len(tocExts) == 1 && tocExts[0] == "" || len(tocExts) == 2 && tocExts[0] == "" && tocExts[1] == "" { + isTocMarkup = true + } else { + fileExt := strings.ToLower(filepath.Ext(blob.Name())) + if fileExt != "" { + for _, tExt := range tocExts { + if tExt == fileExt { + isTocMarkup = true + break + } + } + } + } + ctx.Data["IsTocMarkup"] = isTocMarkup ctx.Data["IsMarkup"] = true ctx.Data["FileContent"] = string(markup.Render(blob.Name(), buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas())) } else if readmeExist { diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 56734e6030da2..d89f9e4cdc236 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -45,7 +45,7 @@
    -
    +
    {{if .IsMarkup}} {{if .FileContent}}{{.FileContent | Safe}}{{end}} {{else if .IsRenderedHTML}} From 588fcca7c18e9dc2da9ad0d6e9d48195e2af5fd2 Mon Sep 17 00:00:00 2001 From: Michael Gnehr Date: Thu, 13 Jun 2019 18:30:27 +0200 Subject: [PATCH 12/17] move toc beside markdown container on large screens >1760px Signed-off-by: Michael Gnehr --- public/css/index.css | 6 ++++-- public/less/_markdown.less | 9 ++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/public/css/index.css b/public/css/index.css index 654e2f085a90d..e0104fecaea1a 100644 --- a/public/css/index.css +++ b/public/css/index.css @@ -207,7 +207,7 @@ footer .ui.left,footer .ui.right{line-height:40px} .ui.tabular.menu .item{color:rgba(0,0,0,.5)} .ui.tabular.menu .item:hover{color:rgba(0,0,0,.8)} .ui.tabular.menu .item.active{color:rgba(0,0,0,.9)} -.markdown:not(code){overflow:hidden;font-size:16px;line-height:1.6!important;word-wrap:break-word} +.markdown:not(code){font-size:16px;line-height:1.6!important;word-wrap:break-word} .markdown:not(code).ui.segment{padding:3em} .markdown:not(code).file-view{padding:2em 2em 2em!important} .markdown:not(code)>:first-child{margin-top:0!important} @@ -288,9 +288,11 @@ footer .ui.left,footer .ui.right{line-height:40px} .markdown:not(code) .csv-data th{font-weight:700;background:#f8f8f8;border-top:0} .markdown:not(code) .ui.list .list,.markdown:not(code) ol.ui.list ol,.markdown:not(code) ul.ui.list ul{padding-left:2em} .markdown:not(code) .auto-toc-wrapper{position:relative;padding:0 0 7px 20px;float:right;background:#fff;z-index:1} +@media only screen and (min-width:1760px){.markdown:not(code) .auto-toc-wrapper{right:-31%;width:26%;padding:0;margin-top:-3em!important;margin-left:-26%} +} .markdown:not(code) .auto-toc-container{padding:7px;border:1px solid #d4d4d5;border-radius:5px} .markdown:not(code) .auto-toc-container h2{padding:.3em;font-size:1.65em} -.markdown:not(code) .auto-toc-clear{clear:both} +.markdown:not(code) .auto-toc-clear{clear:both;margin-bottom:-20px!important} .home .logo{max-width:220px} @media only screen and (max-width:767px){.home .hero h1{font-size:3.5em} .home .hero h2{font-size:2em} diff --git a/public/less/_markdown.less b/public/less/_markdown.less index f12b70b4fc860..5571433554920 100644 --- a/public/less/_markdown.less +++ b/public/less/_markdown.less @@ -1,5 +1,4 @@ .markdown:not(code) { - overflow: hidden; font-size: 16px; line-height: 1.6 !important; word-wrap: break-word; @@ -503,6 +502,13 @@ float: right; background: #ffffff; z-index: 1; + @media only screen and (min-width:1760px) { + right: -31%; + width: 26%; + padding: 0; + margin-top: -3em !important; + margin-left: -26%; + } } .auto-toc-container { @@ -518,5 +524,6 @@ .auto-toc-clear { clear: both; + margin-bottom: -20px !important; } } From e91b8c5e942a2d1fdde434d7fd1c5e1987be9f75 Mon Sep 17 00:00:00 2001 From: Michael Gnehr Date: Fri, 14 Jun 2019 15:35:00 +0200 Subject: [PATCH 13/17] rename configuration variables 'markdown' to 'markup' --- custom/conf/app.ini.sample | 14 +++++----- .../doc/advanced/config-cheat-sheet.en-us.md | 9 +++---- models/migrations/v88.go | 14 +++++----- models/repo.go | 8 +++--- modules/auth/repo_form.go | 4 +-- modules/setting/setting.go | 26 +++++++++---------- options/locale/locale_en-US.ini | 4 +-- routers/repo/setting.go | 8 +++--- routers/repo/view.go | 4 +-- templates/repo/settings/options.tmpl | 8 +++--- templates/repo/view_file.tmpl | 2 +- 11 files changed, 49 insertions(+), 52 deletions(-) diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample index 372f362601f85..13d46b342fbc7 100644 --- a/custom/conf/app.ini.sample +++ b/custom/conf/app.ini.sample @@ -147,13 +147,13 @@ CUSTOM_URL_SCHEMES = FILE_EXTENSIONS = .md,.markdown,.mdown,.mkd ; Create Table of Contents based on headlines on wiki page (does not alter file) DEFAULT_TOC_WIKI_FILE = true -; Create Table of Contents on all rendered markdown files (does not alter file), enabled: overwrites DEFAULT_TOC_MARKDOWN_BY_FLAG -DEFAULT_TOC_MARKDOWN_ALWAYS = false -; Create Table of Contents on renderes markdown files if line '%%TOC%%' is present -DEFAULT_TOC_MARKDOWN_BY_FLAG = true -; List of markup file extensions that TOC should be created on, set to not interfere with external renderers -; Separate the extensions with a comma. To ignore file extension check, just put a comma -TOC_MARKDOWN_FILE_EXTENSIONS = .md,.markdown,.mdown,.mkd,.org +; Create 'Table of Contents' on all rendered markup files. This does not alter files. Enabled: overwrites DEFAULT_TOC_MARKUP_BY_FLAG +DEFAULT_TOC_MARKUP_ALWAYS = false +; Create 'Table of Contents' on rendered markup files if line '%%TOC%%' is present. +DEFAULT_TOC_MARKUP_BY_FLAG = true +; List of markup file extensions that TOC should be created on, set to not interfere with external markup renderer. +; Separate the extensions with a comma. To ignore file extension check, just put a comma. +TOC_MARKUP_FILE_EXTENSIONS = .md,.markdown,.mdown,.mkd,.org [server] ; The protocol the server listens on. One of 'http', 'https', 'unix' or 'fcgi'. diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index 1d346d6ff8082..f9ae6afbc0e3f 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -109,12 +109,9 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`. - `ENABLE_HARD_LINE_BREAK`: **false**: Enable Markdown's hard line break extension. - `DEFAULT_TOC_WIKI_FILE`: **true**: Create Table of Contents based on headlines on wiki page (does not alter file) -- `DEFAULT_TOC_MARKDOWN_ALWAYS`: **false** : Create Table of Contents on all rendered markdown files - (does not alter file), enabled: overwrites `TOC_MARKDOWN_BY_FLAG` -- `DEFAULT_TOC_MARKDOWN_BY_FLAG`: **true** : Create Table of Contents on renderes markdown files if - line '%%TOC%%' is present -- `TOC_MARKDOWN_FILE_EXTENSIONS`: **.md,.markdown,.mdown,.mkd,.org** : List of markup file extensions - that TOC should be created on, set to not interfere with external renderers. +- `DEFAULT_TOC_MARKUP_ALWAYS`: **false** : Create 'Table of Contents' on all rendered markup files. This does not alter files. Enabled: overwrites `DEFAULT_TOC_MARKUP_BY_FLAG` +- `DEFAULT_TOC_MARKUP_BY_FLAG`: **true** : Create 'Table of Contents' on rendered markup files if line `%%TOC%%` is present. +- `TOC_MARKUP_FILE_EXTENSIONS`: **.md,.markdown,.mdown,.mkd,.org** : List of markup file extensions that TOC should be created on, set to not interfere with external markup renderer. Separate the extensions with a comma. To ignore file extension check, just put a comma. ## Server (`server`) diff --git a/models/migrations/v88.go b/models/migrations/v88.go index a7228d224a82d..52295efeb8979 100644 --- a/models/migrations/v88.go +++ b/models/migrations/v88.go @@ -13,9 +13,9 @@ import ( func addCanTocOnWikiAndMarkdown(x *xorm.Engine) error { type Repository struct { - TocWikiFile bool `xorm:"NOT NULL DEFAULT true"` - TocMarkdownAlways bool `xorm:"NOT NULL DEFAULT false"` - TocMarkdownByFlag bool `xorm:"NOT NULL DEFAULT true"` + TocWikiFile bool `xorm:"NOT NULL DEFAULT true"` + TocMarkupAlways bool `xorm:"NOT NULL DEFAULT false"` + TocMarkupByFlag bool `xorm:"NOT NULL DEFAULT true"` } if err := x.Sync2(new(Repository)); err != nil { @@ -27,13 +27,13 @@ func addCanTocOnWikiAndMarkdown(x *xorm.Engine) error { return err } - if _, err := x.Exec("UPDATE repository SET toc_markdown_always = ?", - setting.Markdown.DefaultTocMarkdownAlways); err != nil { + if _, err := x.Exec("UPDATE repository SET toc_markup_always = ?", + setting.Markdown.DefaultTocMarkupAlways); err != nil { return err } - if _, err := x.Exec("UPDATE repository SET toc_markdown_by_flag = ?", - setting.Markdown.DefaultTocMarkdownByFlag); err != nil { + if _, err := x.Exec("UPDATE repository SET toc_markup_by_flag = ?", + setting.Markdown.DefaultTocMarkupByFlag); err != nil { return err } return nil diff --git a/models/repo.go b/models/repo.go index ed21931587d90..8008be1ff344f 100644 --- a/models/repo.go +++ b/models/repo.go @@ -170,8 +170,8 @@ type Repository struct { IsFsckEnabled bool `xorm:"NOT NULL DEFAULT true"` CloseIssuesViaCommitInAnyBranch bool `xorm:"NOT NULL DEFAULT false"` TocWikiFile bool `xorm:"NOT NULL DEFAULT true"` - TocMarkdownAlways bool `xorm:"NOT NULL DEFAULT false"` - TocMarkdownByFlag bool `xorm:"NOT NULL DEFAULT true"` + TocMarkupAlways bool `xorm:"NOT NULL DEFAULT false"` + TocMarkupByFlag bool `xorm:"NOT NULL DEFAULT true"` Topics []string `xorm:"TEXT JSON"` // Avatar: ID(10-20)-md5(32) - must fit into 64 symbols @@ -1368,8 +1368,8 @@ func CreateRepository(doer, u *User, opts CreateRepoOptions) (_ *Repository, err IsFsckEnabled: !opts.IsMirror, CloseIssuesViaCommitInAnyBranch: setting.Repository.DefaultCloseIssuesViaCommitsInAnyBranch, TocWikiFile: setting.Markdown.DefaultTocWikiFile, - TocMarkdownAlways: setting.Markdown.DefaultTocMarkdownAlways, - TocMarkdownByFlag: setting.Markdown.DefaultTocMarkdownByFlag, + TocMarkupAlways: setting.Markdown.DefaultTocMarkupAlways, + TocMarkupByFlag: setting.Markdown.DefaultTocMarkupByFlag, } sess := x.NewSession() diff --git a/modules/auth/repo_form.go b/modules/auth/repo_form.go index f9302c2b24b08..0d45e267e7a6c 100644 --- a/modules/auth/repo_form.go +++ b/modules/auth/repo_form.go @@ -126,8 +126,8 @@ type RepoSettingForm struct { EnableIssueDependencies bool IsArchived bool TocWikiFile bool - TocMarkdownAlways bool - TocMarkdownByFlag bool + TocMarkupAlways bool + TocMarkupByFlag bool // Admin settings EnableHealthCheck bool diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 44b66200a3a0d..b19ee2e4e22c5 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -236,20 +236,20 @@ var ( // Markdown settings Markdown = struct { - EnableHardLineBreak bool - CustomURLSchemes []string `ini:"CUSTOM_URL_SCHEMES"` - FileExtensions []string - DefaultTocWikiFile bool - DefaultTocMarkdownAlways bool - DefaultTocMarkdownByFlag bool - TocMarkdownFileExtensions []string + EnableHardLineBreak bool + CustomURLSchemes []string `ini:"CUSTOM_URL_SCHEMES"` + FileExtensions []string + DefaultTocWikiFile bool + DefaultTocMarkupAlways bool + DefaultTocMarkupByFlag bool + TocMarkupFileExtensions []string }{ - EnableHardLineBreak: false, - FileExtensions: strings.Split(".md,.markdown,.mdown,.mkd", ","), - DefaultTocWikiFile: true, - DefaultTocMarkdownAlways: false, - DefaultTocMarkdownByFlag: true, - TocMarkdownFileExtensions: strings.Split(".md,.markdown,.mdown,.mkd,.org", ","), + EnableHardLineBreak: false, + FileExtensions: strings.Split(".md,.markdown,.mdown,.mkd", ","), + DefaultTocWikiFile: true, + DefaultTocMarkupAlways: false, + DefaultTocMarkupByFlag: true, + TocMarkupFileExtensions: strings.Split(".md,.markdown,.mdown,.mkd,.org", ","), } // Admin settings diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index f398228ad3acd..1114ffc348298 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1151,8 +1151,8 @@ settings.admin_enable_health_check = Enable Repository Health Checks (git fsck) settings.admin_enable_close_issues_via_commit_in_any_branch = Close an issue via a commit made in a non default branch settings.toc.toc = Table of Contents settings.toc.toc_wiki_file = Enable autogenerated TOC on wikipages (based on headlines) (does not alter files) -settings.toc.toc_markdown_always = Enable autogenerated TOC on all markdown files (based on headlines) (does not alter files) -settings.toc.toc_markdown_by_flag = Enable autogenerated TOC on markdown files which contain '%%TOC%%' (based on headlines) (does not alter files) +settings.toc.toc_markup_always = Enable autogenerated TOC on all markup files (based on headlines) (does not alter files) +settings.toc.toc_markup_by_flag = Enable autogenerated TOC on markup files which contain keyword '%%TOC%%' (based on headlines) (does not alter files) settings.danger_zone = Danger Zone settings.new_owner_has_same_repo = The new owner already has a repository with same name. Please choose another name. settings.convert = Convert to Regular Repository diff --git a/routers/repo/setting.go b/routers/repo/setting.go index fadf1b9211400..8a2157059774a 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -299,12 +299,12 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { repo.TocWikiFile = form.TocWikiFile } - if repo.TocMarkdownAlways != form.TocMarkdownAlways { - repo.TocMarkdownAlways = form.TocMarkdownAlways + if repo.TocMarkupAlways != form.TocMarkupAlways { + repo.TocMarkupAlways = form.TocMarkupAlways } - if repo.TocMarkdownByFlag != form.TocMarkdownByFlag { - repo.TocMarkdownByFlag = form.TocMarkdownByFlag + if repo.TocMarkupByFlag != form.TocMarkupByFlag { + repo.TocMarkupByFlag = form.TocMarkupByFlag } if err := models.UpdateRepository(repo, false); err != nil { diff --git a/routers/repo/view.go b/routers/repo/view.go index 4b17375a7c698..b75ce5d3dec1e 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -165,7 +165,7 @@ func renderDirectory(ctx *context.Context, treeLink string) { if markup.Type(readmeFile.Name()) != "" { // Check if extension matches TOC file list - tocExts := setting.Markdown.TocMarkdownFileExtensions + tocExts := setting.Markdown.TocMarkupFileExtensions isTocMarkup := false if len(tocExts) == 0 || len(tocExts) == 1 && tocExts[0] == "" || len(tocExts) == 2 && tocExts[0] == "" && tocExts[1] == "" { isTocMarkup = true @@ -302,7 +302,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st ctx.Data["ReadmeExist"] = readmeExist if markup.Type(blob.Name()) != "" { // Check if extension matches TOC file list - tocExts := setting.Markdown.TocMarkdownFileExtensions + tocExts := setting.Markdown.TocMarkupFileExtensions isTocMarkup := false if len(tocExts) == 0 || len(tocExts) == 1 && tocExts[0] == "" || len(tocExts) == 2 && tocExts[0] == "" && tocExts[1] == "" { isTocMarkup = true diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index dd6fc4b9e074f..d941ce3814792 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -275,14 +275,14 @@
    - - + +
    - - + +
    diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index d89f9e4cdc236..db4e8824e125c 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -45,7 +45,7 @@
    -
    +
    {{if .IsMarkup}} {{if .FileContent}}{{.FileContent | Safe}}{{end}} {{else if .IsRenderedHTML}} From 8dd08af927fcc5c0d7e8957ecc07fbdb3089954e Mon Sep 17 00:00:00 2001 From: Michael Gnehr Date: Mon, 17 Jun 2019 02:04:20 +0200 Subject: [PATCH 14/17] set fixed width on small mobile screens Signed-off-by: Michael Gnehr --- public/css/index.css | 2 ++ public/less/_markdown.less | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/public/css/index.css b/public/css/index.css index e0104fecaea1a..1e46472aa6a70 100644 --- a/public/css/index.css +++ b/public/css/index.css @@ -290,6 +290,8 @@ footer .ui.left,footer .ui.right{line-height:40px} .markdown:not(code) .auto-toc-wrapper{position:relative;padding:0 0 7px 20px;float:right;background:#fff;z-index:1} @media only screen and (min-width:1760px){.markdown:not(code) .auto-toc-wrapper{right:-31%;width:26%;padding:0;margin-top:-3em!important;margin-left:-26%} } +@media only screen and (max-width:479px){.markdown:not(code) .auto-toc-wrapper{float:none;width:100%;padding:0;margin-bottom:1em} +} .markdown:not(code) .auto-toc-container{padding:7px;border:1px solid #d4d4d5;border-radius:5px} .markdown:not(code) .auto-toc-container h2{padding:.3em;font-size:1.65em} .markdown:not(code) .auto-toc-clear{clear:both;margin-bottom:-20px!important} diff --git a/public/less/_markdown.less b/public/less/_markdown.less index 5571433554920..f824d2e17f869 100644 --- a/public/less/_markdown.less +++ b/public/less/_markdown.less @@ -509,6 +509,14 @@ margin-top: -3em !important; margin-left: -26%; } + + @media only screen and (max-width:479px) { + float: none; + width: 100%; + padding: 0; + margin-bottom: 1em; + } + } .auto-toc-container { From 22b82ffb43c8a2e84d76ad97f3a6ef1ec6ae5879 Mon Sep 17 00:00:00 2001 From: Michael Gnehr Date: Fri, 28 Jun 2019 00:40:18 +0200 Subject: [PATCH 15/17] fix css lint errors Signed-off-by: Michael Gnehr --- public/less/_markdown.less | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/public/less/_markdown.less b/public/less/_markdown.less index 4cf378d1d418a..3f5920bc6d901 100644 --- a/public/less/_markdown.less +++ b/public/less/_markdown.less @@ -499,7 +499,8 @@ float: right; background: #ffffff; z-index: 1; - @media only screen and (min-width:1760px) { + + @media only screen and (min-width: 1760px) { right: -31%; width: 26%; padding: 0; @@ -507,7 +508,7 @@ margin-left: -26%; } - @media only screen and (max-width:479px) { + @media only screen and (max-width: 479px) { float: none; width: 100%; padding: 0; From 0607c0a2892e9f06f6b44bc2958833356e20d16e Mon Sep 17 00:00:00 2001 From: Michael Gnehr Date: Sun, 7 Jul 2019 00:49:22 +0200 Subject: [PATCH 16/17] rename migration file Signed-off-by: Michael Gnehr --- models/migrations/{v88.go => v89.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename models/migrations/{v88.go => v89.go} (100%) diff --git a/models/migrations/v88.go b/models/migrations/v89.go similarity index 100% rename from models/migrations/v88.go rename to models/migrations/v89.go From bd83992f1780daf238126e615ecb610fde8dc5b7 Mon Sep 17 00:00:00 2001 From: Michael Gnehr Date: Mon, 8 Jul 2019 15:11:06 +0200 Subject: [PATCH 17/17] git rename migration file to resolve confilcts Signed-off-by: Michael Gnehr --- models/migrations/migrations.go | 2 +- models/migrations/{v89.go => v90.go} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename models/migrations/{v89.go => v90.go} (100%) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 2515a28f1137d..4ffcf9726ec45 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -232,7 +232,7 @@ var migrations = []Migration{ NewMigration("add avatar field to repository", addAvatarFieldToRepository), // v88 -> v89 NewMigration("add commit status context field to commit_status", addCommitStatusContext), - // v89 -> v90 + // v90 -> v91 NewMigration("add toc on wiki and markedown", addCanTocOnWikiAndMarkdown), } diff --git a/models/migrations/v89.go b/models/migrations/v90.go similarity index 100% rename from models/migrations/v89.go rename to models/migrations/v90.go