Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.

Add searchbar component#28

Closed
nusjzx wants to merge 8 commits intoMarkBind:masterfrom
nusjzx:162-search-for-headings
Closed

Add searchbar component#28
nusjzx wants to merge 8 commits intoMarkBind:masterfrom
nusjzx:162-search-for-headings

Conversation

@nusjzx
Copy link

@nusjzx nusjzx commented May 23, 2018

What is the purpose of this pull request? (put "X" next to an item, remove the rest)

• [] Documentation update
• [ ] Bug fix
• [ ] New feature
• [x] Enhancement to an existing feature
• [ ] Other, please explain:

Related MarkBind/markbind#162, MarkBind/markbind#204

What is the rationale for this request?
highlighting and extracting the search result helps users to identify the results they want easily,
searching for headings and jump to the headings on click helpers users to go to website sections easily.

What changes did you make? (Give an overview)
highlightAndExtract function will extract the matching results then highlight the phrases.
for jumping to a specific heading, we have one entry for each heading(suggested by Prof Damith).
Thus, Instead of processing on a single string that combines title, keywords and headings, we match headings first, then title and keywords.

Testing instructions:

  1. in vue-strap npm run build

  2. copy paste vue-strap.min.js to markbind

  3. After cloning the CS3281 website, go to common/header.md, and replace the use of with :

  4. test on 3281 website

@acjh
Copy link

acjh commented May 23, 2018

Resolves #162 and #204

Change to:

Related: MarkBind/markbind#162, MarkBind/markbind#204

Use "Resolves" if only need to upgrade vue-strap.min.js, i.e. no corresponding change is required.

@nusjzx
Copy link
Author

nusjzx commented May 23, 2018

I add a searchbar.vue which will change the vue-strap.min.js. @acjh

@acjh
Copy link

acjh commented May 23, 2018

How is that relevant?

@nusjzx
Copy link
Author

nusjzx commented May 23, 2018

what do you mean by upgrade vue-strap.min.js

@acjh
Copy link

acjh commented May 23, 2018

I mean upgrade vue-strap.min.js in the MarkBind/markbind repository.

If corresponding change is required, then this PR alone does not resolve the issues.
If you use the Resolves keyword, then merging this PR would incorrectly close them.

@nusjzx
Copy link
Author

nusjzx commented May 23, 2018

Ok, get it

filters: {
highlight (value, phrase) {
return value.replace(new RegExp('(' + phrase + ')', 'gi'), '<strong>$1</strong>')
return value.replace(new RegExp('(' + phrase + ')', 'gi'), '<mark>$1</mark>')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation here is not necessary (we use 2 spacing per level).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nusjzx This change is not used right?

Copy link
Member

@yamgent yamgent May 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used here in the associated markbind's PR, together with the new filter highlightAndExtract:

https://github.com/nusjzx/markbind/blob/56f8b2dd950aa628d0de332cab6d1cacdcf597dd/asset/js/setup.js#L24-L25

The reason is we do the highlighting differently for headings and for keywords.

Headings: Keep everything
Keywords: Only the matching keyword is conserved, with the matching characters highlighted.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned last week, shall we create a mark filter instead?

var query = this.value.toLowerCase()
var hasHeadingMatch = false
var title = value.title.toLowerCase()
var keywords = value.keywords? value.keywords.toLowerCase(): ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to have spacing before the ? and :. I.e.

value.keywords ? value.keywords.toLowerCase() : ""

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we use single quotes for strings, '' rather than "".

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a new file, let's use eslint.
Let me see if I can configure it. For now, you can copy-paste it into a .js file in MarkBind.
We can extend this to all custom files (components) later.

var src = value.src
if (Object.keys(headings).length !== 0) {
for (var key in headings) {
var heading = headings[key].toLowerCase();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation seems to be off here.

var matches = []
var words
this.data.map(value => {
var query = this.value.toLowerCase()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having to convert to lower case toLowerCase(), maybe we can use search() instead of index()?

So instead of: heading.indexOf(query), we use heading.search(new RegExp(query, "i"))). The "i" will make the search case-insensitive.

Hopefully this will make the code clearer.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nusjzx Is it necessary to modify primitiveData?

@yamgent yamgent changed the title 162 search for headings Add searchbar component May 23, 2018
src/index.js Outdated
import tipBox from './TipBox.vue'
import trigger from './trigger.vue'
import typeahead from './Typeahead.vue'
import searchbar from './Searchbar.vue'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move above import tipBox

src/index.js Outdated
trigger,
typeahead
typeahead,
searchbar
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move above trigger

filters: {
highlight (value, phrase) {
return value.replace(new RegExp('(' + phrase + ')', 'gi'), '<strong>$1</strong>')
return value.replace(new RegExp('(' + phrase + ')', 'gi'), '<mark>$1</mark>')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nusjzx This change is not used right?

var matches = []
var words
this.data.map(value => {
var query = this.value.toLowerCase()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nusjzx Is it necessary to modify primitiveData?

var query = this.value.toLowerCase()
var hasHeadingMatch = false
var title = value.title.toLowerCase()
var keywords = value.keywords? value.keywords.toLowerCase(): ""
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a new file, let's use eslint.
Let me see if I can configure it. For now, you can copy-paste it into a .js file in MarkBind.
We can extend this to all custom files (components) later.

@nusjzx
Copy link
Author

nusjzx commented May 23, 2018

@acjh 1. the change is used, as we still need highlight filter
2. if prof want each headings each entry, we cannot process on the string only, we need the object

this.data.map(value => {
var query = new RegExp(this.value, 'i')
var hasHeadingMatch = false
var title = value.title
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have gotten rid of the .toLowerCase(), the assignment might no longer be necessary, just use value.title below. E.g.:

matches.push({'title': value.title,

Same thing for keywords and headings.

var query = new RegExp(this.value, 'i')
var hasHeadingMatch = false
var title = value.title
var keywords = value.keywords ? value.keywords : ''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use || instead. I.e.:

const keywords = value.keywords || '';

for (var key in headings) {
var heading = headings[key]
if (heading.search(query) !== -1) {
hasHeadingMatch = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasHeadingMatch -> hasMatchingHeading

primitiveData() {
if (this.data) {
var matches = []
var words
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this declaration, declare this below when it is initialized, so it is something like:

const words = keywords ? keywords + title : title

var matches = []
var words
this.data.map(value => {
var query = new RegExp(this.value, 'i')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the variable does not change its value after initialization, you should use const instead of var:

const query = new RegExp(this.value, 'i')

Same for the rest of the variables below.

tooltip,
pic,
trigger,
searchbar,
Copy link
Member

@yamgent yamgent May 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation here is tab rather than space. I think your IDE might be set to tabs rather than space?

@acjh acjh mentioned this pull request May 23, 2018
@nusjzx nusjzx closed this May 23, 2018
@nusjzx nusjzx deleted the 162-search-for-headings branch May 23, 2018 12:22
@yamgent yamgent mentioned this pull request May 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants